simplydi / simplytemplate
非常简单的原生PHP模板引擎
dev-main
2023-08-04 16:19 UTC
This package is auto-updated.
Last update: 2024-09-04 18:37:17 UTC
README
这是一个非常简单的原生PHP模板引擎,适用于小型项目。
注意
- 仅适用于小型项目;对于更复杂的任务请使用 League Plate、Twig、Latte 等工具。
- 目前处于开发中 = 容易出错
- 为了学习目的而创建
使用方法
创建要渲染的模板。使用 @extend=layoutName
指令扩展布局。
您可以使用 $this->esc($var)
跳过变量。
// Template to render: "page.php" @extend=_layout <h1><?= $title ?></h1> <p><?= $content ?></p> <?= $this->esc("<p>hello</p>");?>
_layout.php:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title><?= $title ?></title> </head> <body> <header> this is header </header> <main> <?= $content ?> </main> <aside> <?php include('_sidebar.php') ?> </aside> <footer> footer here </footer> </body> </html>
要包含任何部分,只需使用 PHP 函数 include()
。
使用模板引擎进行渲染
<?php include_once 'vendor/autoload.php'; $templateDir = __DIR__ . '/views'; $templateExtension = 'php'; $template = new \SimplyDi\SimplyTemplate\Engine( $templateDir, $templateExtension ); $data = [ 'title' => 'My Page Title', 'content' => 'This is the main content of the page.', ]; // get the template html via renderer $output = $template->render('page', $data); echo $output; // outputs the html