edevelops/native-tpl-heir
Composer 安装命令:
composer require edevelops/native-tpl-heir
包简介
Tiny plain PHP native template inheritance library
README 文档
README
Tiny plain PHP native template inheritance library
Inspired by phpti
Features
- About 35 lines of code at all
- No needs to know any template language other than PHP
- The entire API is just 3 global functions!
- Overridable blocks are not executed!
- No
ob_*buffering used
Usage
Without composer: just copy/include tiny source directrly.
Using composer:
$ composer require edevelops/native-tpl-heir
Then need to touch NativeTplHeir class to trigger auto-loading. For example
new NativeTplHeir();
API
slot(string $name, Closure $render = null)- the output point of the block in the template.$name- unique block name.$render- optional callback for rendering.block(string $name, Closure $render)- defining block to output in theslotof parent template.$name- unique block name.$render- callback for rendering.super()- calling the overridden parent's callback for rendering.
Inheritance
Just include parent template at the end of file.
Example
root.php:
?> <!DOCTYPE html> <html> <head> <title><?php slot('title')?></title> </head> <body> <div id="root"> <?php slot('body', function () { ?> <p>'body' :: root.php</p> <?php }) ?> </div> </body> </html>
two-columns.php:
block('title', function () { ?> Title :: two-columns.php <?php }); block('body', function () { ?> <div id="two-columnts"> <div id="main"> <?php slot('main', function () { ?> <p>'main' :: two-columns.php</p> <?php }) ?> </div> <div id="side"> <?php slot('side', function () { ?> <p>'side' :: two-columns.php</p> <?php }) ?> </div> </div> <div id="footer"> <?php slot('footer', function () { ?> <p>'footer' :: two-columns.php</p> <?php }) ?> </div> <?php }); include __DIR__.'/root.php';
index.php:
block('title', function () { ?> 'title' :: index.php <?php }); block('side', function () { ?> <p>'side' :: index.php</p> <?php }); block('main', function () { ?> <div id="main-index-override"> <?php super() ?> </div> <?php }); block('main', function () { ?> <div id="main-index"> <?php super() ?> </div> <?php }); include __DIR__.'/two-columns.php';
Rendered result (formatted for better readability):
<!DOCTYPE html> <html> <head> <title> 'title' :: index.php </title> </head> <body> <div id="root"> <div id="two-columnts"> <div id="main"> <div id="main-index-override"> <div id="main-index"> <p>'main' :: two-columns.php</p> </div> </div> </div> <div id="side"> <p>'side' :: index.php</p> </div> </div> <div id="footer"> <p>'footer' :: two-columns.php</p> </div> </div> </body> </html>
统计信息
- 总下载量: 8
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-05-30