anpv1 / minplate
PHP的迷你模板引擎
v1.0.3
2019-11-10 14:16 UTC
This package is auto-updated.
Last update: 2024-09-11 00:37:07 UTC
README
PHP的迷你模板引擎
简介
minplate是一个用于PHP的迷你模板引擎,它具有非常简单的API,易于学习。minplate使用纯PHP编写,因此运行速度快,无需学习新的语法。minplate支持以下功能:
- 为模板分配变量。
- 在布局中创建可以覆盖的块。
- 包含/继承其他模板文件。
- 使用add_path函数允许覆盖模板目录。最新插入的路径将优先使用。
就这么多!
API
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
<?php use MinPlate\Template; $template = new Template('../templates'); $template->assign('name', 'An'); echo $template->render('page.tpl');
layout.tpl
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title><?php $this->block('title'); ?><?php $this->end_block('title'); ?></title> </head> <body> <?php $this->block('content'); ?> <?php $this->end_block('content'); ?> </body> </html>
page.tpl
<?php $this->include('layout.tpl'); ?> <?php $this->block('title'); ?> Hello world! <?php $this->end_block('title'); ?> <?php $this->block('content'); ?> <section class="section"> <div class="container"> <h1 class="title"> <?php $this->block('content_title'); ?> Hello <?= $name ?>! <?php $this->end_block('content_title'); ?> </h1> <p class="subtitle"> My first website! </p> </div> </section> <?php $this->end_block('content'); ?>
最终输出
<!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>