Simple and fast template engine
-
Define the default path in which the engine will search for view files:
Pure\Template\View::namespace( $default_path );
-
Instantiate a view object:
$view = new Pure\Template\View(); // or $view = new Pure\Template\View( array('param1' => 'value1', ... , 'paramN' => 'valueN') );
-
Set params:
$view->paramJ = 'valueJ';
In this way it is possibile to define other parameters outside the default constructor.
-
Clear params:
$view->clear();
-
Render the output:
$view->render( $filename, // the file palced inside the base path $direct_output = true, // if true, the output is displayed $dont_compute = false // if true, no engine extensions are applied );
-
Instead of instantiate the view object, it is possibile to directly output a view by a static function:
Pure\Template\View::make( $filename, $params = array(), $direct_output = true, $dont_compute = false );
-
How to locate views in different paths and render them using namespaces
It is possible to define several namespaces, each namespace refers to a certain path
Pure\Template\View::namespace('path/views'); // define the base namespace Pure\Template\View::namespace('path/views/auth', 'auth'); // define the auth namespace
Once the namespaces are defined, it is possible to load views using the syntax
"namespace::view_filename"
For example:
Pure\Template\View::make('welcome.php'); // file: path/views/welcome.php Pure\Template\View::make('auth::login.php'); // file: path/views/auth/login.php
- Define a view in path: views/example.php
<html> <head> <title>Example</title> </head> <body> <?php echo $foo; ?> </body> </html>
- Render the view:
use Pure\Template\View; // Set the default path View::namespace('views'); $view = new View(); $view->foo = "Hello View!"; // set the param foo $result = $view->render('example.php');
- The output will be this:
<html> <head> <title>Example</title> </head> <body> Hello View! </body> </html>
In the last example we used
<body>
<?php echo $foo; ?>
</body>
to render the foo variable, which is defined as:
$view = new View();
$view->foo = "Hello View!"; // set the param foo
If during the render phase, the argument $dont_compute is set to false, the view engine extensions are called. Which means that more features are available.
- Render paramas in fast way:
<body> {{ $foo }} </body>
Another Pure Template extension let to extend views and override contents.
- To extend a view:
the @extends must be the first statement
@extends('view_filename')
- Sections have to be defined in parent view:
@section('section_name')
- Sections can be override as follow:
@begin('section_name') <h1> HTML content </h1> @end
- Define a parent template placed in views/template.php
<html> <head> <title>Example</title> </head> <body> @section('content') </body> </html>
- Define a new view derived by this template:
@extends('template.php') @begin('content') <p>Hello View!</p> @end
- The output will be this:
<html> <head> <title>Example</title> </head> <body> <p>Hello View</p> </body> </html>