A mini template engine for PHP
A mini template engine for PHP, (*1)
minplate is a mini template engine for PHP which has a very simple API and easy to learn. minplate use pure PHP so it's super fast and you don't have to learn new syntax. minplate support the below features: - assign variables for the template. - creating block in the layout which can be override. - include/inherite other template file. - allow override the template directory with add_path function. The latest inserted path will be used first., (*2)
That's all!, (*3)
function __construct($template_path = './'); function add_path($template_path); function assign(string $variable_name, $value); function include(string $template_name); function block(string $block_name); function end_block(string $block_name); function render(string $template_name, array $data = []);
index.php, (*4)
assign('name', 'An'); echo $template->render('page.tpl'); ``` layout.tpl ```html+phpblock('title'); ?>end_block('title'); ?></title> </head> <body> block('content'); ?> end_block('content'); ?> </body> </html>
page.tpl ```html+php include('layout.tpl'); ?>, (*5)
block('title'); ?> Hello world! end_block('title'); ?>, (*6)
block('content'); ?>
My first website!
, (*8)
block('content_title'); ?>
Hello = $name ?>!
end_block('content_title'); ?>
Final output ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Hello world!</title> </head> <body> <section class="section"> <div class="container"> <h1 class="title">Hello An!</h1> <p class="subtitle">My first website!</p> </div> </section> </body> </html>