Working with PIMF Views and Tempate Engines
PIMF Views contain the HTML that is sent to the user working with your application. By separating your view from the business logic of your application, your code will be cleaner and easier to maintain.
All view-templates are stored within the '/app/MyFirstBlog/templates/' directory and use the PHTML file extension. The PimfView class provides a simple way to retrieve your view-templates and return them to the client.
Following views are available out of the box:
* PimfView for common usage
* PimfViewJson for smooth JSON communication
* PimfViewHaanga is optional and needs HAANGA template engine
* PimfView_Twig is optional and needs TWIG template engine
Pimf_View
Loading the main view of the blog
// use app/MyFirstBlog/_templates/theblog.phtml for viewing
$viewMain = new Pimf_View('theblog.phtml');
// assign data to the template
$viewMain->assign('blog_title', 'This is my firs Blog with PIMF')
->assign('blog_content', $view->render())
->assign('blog_footer', 'A Blog about cool and thin framework');
return $viewMain->render();
Renders a HTML list of all entries which are stored at the sqlite database
// use app/MyFirstBlog/_templates/list.phtml for viewing
$viewAllEntries = new Pimf_View('list.phtml');
$entries = Pimf_Registry::get('em')->entry->getAll();
// assign data to the template
$viewAllEntries->assign('entries', $entries);
echo $this->loadMainView($viewAllEntries);
Pimf_View Partial Helper
The methods partial($template, array $model = array()) and loop($template, array $model = array()) at Pimf_View are used to
render a specified template within its own variable scope. The primary use is for reusable template fragments with which you do not need to
worry about variable name clashes.
Action which handles with partials and uses 'app/MyFirstBlog/templates/booklist.phtml' and 'app/MyFirstBlog/templates/book.phtml' to
render a table.
$data['books'] = array(
array(
'author' => 'Hernando de Soto',
'title' => 'The Mystery of Capitalism'
),
array(
'author' => 'Henry Hazlitt',
'title' => 'Economics in One Lesson'
),
);
$view = new Pimf_View('booklist.phtml');
echo $view->pump($data)->render();
Here how the 'app/MyFirstBlog/_templates/booklist.phtml' looks like:
<?php if ($this->books) : ?>
<table border="1">
<thead>
<tr>
<th>Autor</th>
<th>Title</th>
</tr>
</thead>
<tbody>
<?php echo $this->loop('book.phtml', $this->books); ?>
</tbody>
</table>
<?php endif; ?>
Here how the looping through 'app/MyFirstBlog/_templates/book.phtml' partial looks like:
<tr>
<td><?php echo $this->author; ?></td>
<td><?php echo $this->title; ?></td>
</tr>
PimfViewJson
Sends a data for single entry as a JSON format.
// open new json view
$view = new Pimf_View_Json();
// pump all data to the view and render
$view->pump(array('name' => 'rambo'))->render();
Template Engines
PIMF works great with external template engines like TWIG and HAANGA. Twig is a flexible, fast, and secure template engine for PHP. Haanga
is a template engine that bases on the TWIGs idea and uses Django syntax for parsing the PHTML files. PIMF gives you the posibility to use
them both optionally or parallely. You can use more than one template engine. Go wild!
PimfViewTwig
Before start using it - please add the following code to the end of the config.php file:
'view' => array(
'twig' => array(
'cache' => true, // if compilation caching should be used
'debug' => false, // if set to true, you can display the generated nodes
'auto_reload' => true, // useful to recompile the template whenever the source code changes
),
),
A "Hallo world" action for showing that PIMF works great with TWIG
// use app/MyFirstBlog/_templates/parent.twig for viewing
$view = new Pimf_View_Twig('parent.twig');
// assign data to the template
$view->assign('hello', 'Hello world')
->assign('now', date('d M Y h:i:s', time()));
echo $view->render();
PimfViewHaanga
Before start using it - please add the following code to the end of the config.php file:
'view' => array(
'haanga' => array(
'cache' => true, // if compilation caching should be used
'debug' => false, // if set to true, you can display the generated nodes
'auto_reload' => true, // useful to recompile the template whenever the source code changes
),
),
A "Hallo world" action for showing that PIMF works great with HAANGA.
// use app/MyFirstBlog/_templates/parent.haanga for viewing
$view = new Pimf_View_Haanga('parent.haanga');
// assign data to the template
$view->assign('hello', 'Hello world')
->assign('now', date('d M Y h:i:s', time()));
echo $view->render();