This can be done without subclassing the lithium\template\view\Renderer
class. In the media.php
bootstrap, use the 'handlers'
key with Media::type()
to pass a customized handler to the renderer:
Media::type('html', 'text/html', [
'view' => 'lithium\template\View',
'handlers' => ['date' => function($date) use (&$ctx) {
if (!is_null($date)) {
$ctx['date'] = $date;
}
return $ctx['date'];
}]
]);
Then, $this->date()
becomes available in the views and layouts, e.g.:
//in the default.html.php layout
echo $this->date(); //value passed from /views/controller/index.html.php
//in the views/controller/index.html.php
$this->date(date('Y-m-d'));
As a side note, the $ctx
variable is necessary for accessing the rendering context; see the lithium\template\view\Renderer::_init()
method for where the 'handlers'
array gets merged with the rendering context.