jelix / castor
使用简单语法的模板引擎
v1.1.3
2023-11-08 15:01 UTC
Requires
- php: >=5.6 <8.4
- jelix/simplelocalization: 1.7.*
Requires (Dev)
- phpunit/phpunit: 8.5.*
README
Castor 是一个类似于 PHP 语法的 PHP 模板引擎。
特点
- 一个简单的 API 用于注入数据和生成内容
- 一种语法类似于 PHP 但更简单易学的语言
- 模板可以是文件或简单的字符串
- 高效的生成器:模板文件被“编译”为 PHP 文件
- 沙盒模式,用于获取不受信任的模板(例如,用户在 CMS 中上传的模板)。当然,这种模式的功能较少。
- 插件系统,类似于 Smarty 插件。
- 插件可以针对特定内容类型(HTML、XML、文本等),从而生成正确的内容。
- 一个“元”系统:允许模板向 PHP 代码公开数据。例如,“元”可以是一个用于生成内容的样式表的 URL。
历史
Castor 之前被称为 jTpl,自 2006 年以来一直用于 Jelix 框架。曾经有一个特定的版本,“jTpl 独立版”,存在多年,可以在没有 Jelix 的情况下使用 jTpl,但它从未作为一个稳定版本发布。
2015 年,jTpl 完全从 Jelix 中“提取”出来(从 Jelix 1.7 开始),现在作为名为“Castor”的独立组件提供,并且有真正的稳定版本。
安装
您可以通过 Composer 安装它。在您的项目中
composer require "jelix/castor"
使用方法
模板文件
{! autoescape !}
<h1>{$titre|upper}</h1>
<ul>
{foreach $users as $user}
<li>{$user->name} ({$user->birthday|datetime:'d/m/Y'})
<div>{$user->biography|raw}</div>
</li>
{/foreach}
</ul>
PHP 代码
// directory where compiled templates are stored $cachePath = realpath(__DIR__.'/temp/') . '/'; // directory where templates can be found $templatePath = __DIR__.'/'; // create a configuration object. See its definition to learn about all of its options $config = new \Jelix\Castor\Config($cachePath, $templatePath); // let's create a template engine $tpl = new \Jelix\Castor\Castor($config); // assign some values, so they will be available for the template $users = array( // User in an example class... new User('Tom', '2001-02-01'), new User('Laurent', '1990-03-01'), new User('Bob', '1970-05-25') ); $tpl->assign('users', $users); $tpl->assign('titre', 'This is a test !'); // content is generated from the given template file and returned $content = $tpl->fetch('test.tpl'); // or content is generated from the given template file and sent directly to the browser $tpl->display('test.tpl');
要了解更多信息,请参阅 docs/ 目录。