Custom Views
A Yee application delegates rendering of templates to its view object. A custom view is a subclass of \Yee\View
that implements this interface:
<?php
public render(string $template);
The view object's render
method must return the rendered content of the template specified by its $template
argument. When the custom view’s render method is invoked, it is passed the desired template pathname (relative to the Yee application's “templates.path” setting) as its argument. Here's an example
custom view:
<?php
class CustomView extends \Yee\View
{
public function render($template)
{
return 'The final rendered template';
}
}
The custom view can do whatever it wants internally so long as it returns the template’s rendered output as a string. A custom view makes it easy to integrate popular PHP template systems like Twig or Smarty.
render()
method with $this->data
.
You can browse ready-to-use custom views that work with popular PHP template engines in the Yee-Extras Section on yeephp.com.
Example View
<?php
class CustomView extends \Yee\View
{
public function render($template)
{
// $template === 'show.php'
// $this->data['title'] === 'Sahara'
}
}
Example Integration
If the custom view is not discoverable by a registered autoloader, it must be required before the Yee application is instantiated.
<?php
require 'CustomView.php';
$app = new \Yee\Yee(array(
'view' => new CustomView()
));
$app->get('/books/:id', function ($id) use ($app) {
$app->render('show.php', array('title' => 'Sahara'));
});
$app->execute();