This package is not installable via Composer 1.x, please make sure you upgrade to Composer 2+. Read more about our Composer 1.x deprecation policy.

Windwalker View package

Installs: 22

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 3

Forks: 0

Open Issues: 0


2.1.9 2016-02-12 15:36 UTC


Installation via Composer

Add this to the require block in your composer.json.

    "require": {
        "windwalker/view": "~2.0"

Create A Simple View


AbstractView is very simple, it only need a render method to render what you want.

use Windwalker\View\AbstractView;

class MyView extends AbstractView
    public function render()
        $tmpl = <<<TMPL
# The is a Markdown Article

Hello %s~~~!

        return MyMarkdown::render(sprintf($tmpl, $this->data['foo']));

// Create view and set data
$view = new MyView;

$view->set('foo', 'World');


The Result will be:

<h1>The is a Markdown Article</h1>

Hello World~~~!


SimpleHtmlView can set a php file to render:

use Windwalker\View\SimpleHtmlView;

class MyHtmlView extends SimpleHtmlView
    public function prepare($data)
        // Format dome data
        $data['time'] = $data['time']->format('Y-m-d H:i:s');

        $data['link'] = '/flower/' . OutputFilter::stringUrlSafe($data['name']) . '.html';

$view = new MyHtmlView;

$view->set('time', new DateTime);
$view->set('name', $name);


The template file:


$time = $data['time'];
    Now is: <?php echo $this->escape($time); ?>

    <a href="<?php echo $data['link'] ?>">
        <?php echo $this->escape($data['name']); ?>


HtmlView is more powerful than SimpleHtmlView, we can set Renderer as a render engine into it, and find template in several paths.

use Windwalker\View\HtmlView;

$paths = new SplPriorityQueue;
$paths->insert('path/of/system', 300);
$paths->insert('path/of/theme', 500);

$data = array(
    'time' => new DateTime

$view = new HtmlView($data, new PhpRenderer($paths));

$view->setLayout('foo')->render(); // Will find foo.php in every paths.

See also: Windwalker Renderer

Extends It

use Windwalker\View\HtmlView;
use Windwalker\Renderer\BladeRenderer;

// A Blade View
class BladeHtmlView extends HtmlView
    public function __construct($data = array(), BladeRenderer $renderer = null)
        $renderer = $renderer ? : new BladeRenderer('default/path', array('cache_path' => 'cache/path'))

        parent::__construct($data, $renderer);

// View for different MVC structures
class ArticleHtmlView extends BladeHtmlView
    public function prepare($data)
        $data['time'] = $data['time']->format('Y-m-d H:i:s');

$view = new MyHtmlView;

$view->['time'] = new DateTime; // Use array access

$view->setLayout('template')->render(); // Will find template.blade.php

The Data object

HtmlView use Windwalker\Data\Data as data store, we don't need to worry about data exists or not.


$time = $data['time']; // Exists
$name = $data['name']; // Not exists, just return null.
$title = $data->title; // Also support object access.

See Windwalker Data


JsonView use Registry as data store, we can separate different level by dot(.).

$view = new JsonView;

$view[''] = 'baz';


The result will be:

    "foo": {
        "bar": "baz"