Piece
Simple template engine component for ITCourses Jazz PHP framework, (*1)
How to use:
First of all, create an array of settings for Piece component:, (*2)
// settings array
$settings = [
// folder, where you store your view's
'viewsFolder' => __DIR__ . '/views',
// file extension, which you use for your
// template and views files
'fileExtension' => '.phtml'
];
After that, create an instance of Piece component and pass into it your settings array:, (*3)
use Piece\ViewEngine;
$view = new ViewEngine($settings);
Now, you can run Piece's render() method, which it first argument is name of view and second - array of view's parameters (if any):, (*4)
$view->render('home', ['content'=>'Some content for home page.']);
Supposing our view's files stored in Views folder and template's files in Views/templates folder., (*5)
Views/home.phtml view example:, (*6)
@template('templates/template');
This is view content.
=$content?>, (*7)
In this case, @template('templates/template'); flag tell Piece's render() method where it can find template for this view., (*8)
Views/templates/template.phtml template example:, (*9)
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Home page</title>
</head>
<body>
@embed;
</body>
</html>
@embed; flag tell Piece's render() method where it must inject view's body., (*10)
You can use any PHP construction and variables, in view's files and template's files. All of they are processed by render() method., (*11)
This method also provides XSS protection., (*12)