aleksandr.ru / template-object
另一个简单的模板解析器
Requires
- php: >=5.3.0
This package is auto-updated.
Last update: 2024-09-15 14:27:35 UTC
README
另一个简单的模板解析器。也支持通过composer使用
composer require aleksandr.ru/template-object
特性
- 将标记块作为对象
- 块重复(setBlock 添加新块并返回其句柄)
- 递归块(见以下示例)
- 空块占位符
- 块和主模板中的独立变量
- 模板中的变量数据转义(过滤),见以下标记
- 变量过滤操作:添加、替换、删除自定义过滤器
- 包含和防止递归包含
- 扩展模板和防止递归扩展
标记示例
header.html
<!DOCTYPE html>
<html>
<head>
<title>{{TITLE|raw}}</title>
<script>
alert('{{TITLE|js}}');
</script>
</head>
<body>
page.html
<!-- INCLUDE header.html -->
<table>
<caption>{{TITLE}}</caption>
<tr>
<th>Column-1</th>
<th>Column-2</th>
</tr>
<!-- BEGIN row -->
<tr>
<td>{{COL1}}</td>
<td>{{COL2}}</td>
</tr>
<!-- EMPTY row -->
<tr>
<td colspan=2>No data</td>
</tr>
<!-- END row -->
</table>
<!-- INCLUDE footer.html -->
footer.html
<p>{{MULTILINE|html|nl2br}}</p>
</body>
</html>
代码示例
如果没有使用 'row' 块,将显示 EMPTY 的内容
// WARNING! Since 2.0 the loadTemplate become static and return TemplateObject
// this syntax is not valid any more
// $to = new TemplateObject();
// $to->loadTemplate('page.html');
// Please use following
$to = TemplateObject::loadTemplate('page.html');
$to->setVariable('TITLE', 'this is a title');
for($i=1; $i<=3; $i++) {
$row = $to->setBlock('row');
$row->setVariable('COL1', $i);
$row->setVariable('COL2', "test-$i");
}
$string = "String with \"quotes\" and several lines\n second line\n thitd line";
$to->setVariable('MULTILINE', $string);
$to->showOutput();
扩展模板
从 2.0 版本开始,可以扩展模板。例如
yeild.html
<!DOCTYPE html>
<html>
<head>
<title>{{TITLE}}</title>
</head>
<body>
<header>
<!-- BEGIN head -->
This content will be yeilded
<!-- END head -->
</header>
<section>
<!-- BEGIN content -->
This content will be yeilded
<!-- END content -->
</section>
<footer>
<!-- BEGIN foot -->
This content will be yeilded
<!-- END foot -->
</footer>
</body>
</html>
extend.html
<!-- EXTEND yeild.html -->
<!-- BEGIN head -->
<p>This is the header</p>
<!-- END head -->
<!-- BEGIN content -->
<table border="1">
<caption>This is the content</caption>
<tr>
<th>Column-1</th>
<th>Column-2</th>
</tr>
<!-- BEGIN row -->
<tr>
<td>{{COL|html}}</td>
<td>{{COL|raw}}</td>
</tr>
<!-- EMPTY row -->
<tr>
<td colspan=2>No data</td>
</tr>
<!-- END row -->
</table>
<!-- END content -->
<!-- BEGIN foot -->
<p>This is the footer</p>
<!-- END foot -->
代码相同
$to = TemplateObject::loadTemplate('extend.html');
$to->setVariable('TITLE', "this is a 'title'");
for($i=1; $i<=3; $i++) {
$row = $to->setBlock('row');
$row->setVariable('COL', "test \"$i\"");
}
$to->showOutput();
递归块
从 2.4 版本开始,可以创建递归块。例如
recursive.html
<body>
<!-- BEGIN blockname -->
[a recursive block]
<!-- RECURSION blockname -->
<!-- END blockname -->
</body>
代码
$to = TemplateObject::loadTemplate('recursive.html');
$to->setBlock('blockname')->setBlock('blockname')->setBlock('blockname');
$to->showOutput();
输出
[a recursive block] [a recursive block] [a recursive block]
函数快速参考
static loadTemplate(string $file) : TemplateObject
从文件加载模板。
__construct(string $data = '', string $base_dir = '')
构造函数。
__destruct()
释放并重置资源。
getBlocks() : array
返回模板中找到的所有块。仅返回第一级块,不递归。
getVariables() : array
返回模板中找到的所有变量。仅返回块外的变量。
setBlock(string $blockname) : TemplateObject
设置用于使用的块(将新块添加到标记中并返回句柄)。
setGlobalVariable(string $var, string $val) : boolean
在全局作用域中设置变量。
setVariable(string $var, string $val) : boolean
在标记中设置变量。如果未找到变量,则触发 E_USER_NOTICE。
setVarArray(array $arr)
从数组设置变量,如
array(
'VAR1' => 'value',
'VAR2' => 'another value',
'singleblock' => array('BLOCKVAR1' => 'value1', 'BLOCKVAR2' => 'value2', ...),
'multiblock' => array(
[0] => array('VAR1' => 'val1', 'VAR2' => 'val2'),
[1] => array('VAR1' => 'val3', 'VAR2' => 'val4'),
),
'emptyblock' => NULL,
...)
getOutput() : string
获取设置所有数据的解析模板。
showOutput()
打印设置所有数据的解析模板。
addFilter(string $filter, callable $callback, boolean $overwrite = FALSE) : boolean
添加(或替换)变量过滤器。如果过滤器已存在且未设置 $overwrite,则触发 E_USER_NOTICE。如果给定的 $callback 不可调用,则触发 E_USER_NOTICE。
removeFilter(string $filter) : boolean
删除现有过滤器。如果过滤器不存在,则触发 E_USER_NOTICE。
getForcedFilter() : string
从 2.7 版本开始,获取当前强制过滤器。
setForcedFilter(string $filter) : boolean
从 2.7 版本开始,设置新的强制过滤器。默认设置为 "html"。如果过滤器不存在,则触发 E_USER_WARNING。
强制过滤器是如果没有设置 "raw" 过滤器且未为变量设置相同的过滤器时首先应用的过滤器。强制过滤器可以包含多个元素,例如 html|nl2br
,每个元素都将被添加到变量的过滤器之前,如果未设置。
此机制可能会导致一些向后兼容性问题:在 2.7 之前,{{VAR}}
表示 {{VAR|html}}
,而 {{VAR|js}}
表示只有 {{VAR|js}}
(没有应用 html),从 2.7 开始,{{VAR}}
表示 {{VAR|html}}
,而 {{VAR|js}}
表示 {{VAR|html|js}}
(强制应用 html)。要获取 {{VAR|js}}
的旧行为,您需要添加 "raw" 过滤器,如 {{VAR|raw|js}}
。
更多文档
请参阅代码中的 PhpDoc。
版本历史
- 2.7 强制过滤器代替默认过滤器,直到设置 raw
- 2.6 通过将其设置为非关联数组来移除保留块时的副作用
- 2.5 模板处理加速和新的选项
public $debug = false
- 2.4 通过新的标记
<!-- RECURSION blockname -->
实现递归块 - 2.3 通过
'emptyblock' => NULL
在 setVarArray 中保留空块 - 2.2 块选项:
<!-- BEGIN myblock rsort -->
这些块将以倒序输出,请参阅 BLOCKOPTION_* 常量 - 2.1 全局变量:继承给所有子块
- 2.0 现在一个模板可以通过替换其块以另一个模板扩展另一个模板
- 1.3 能够从加载的模板获取变量和块
- 1.2 支持多个过滤器,如
{{VAR|html|nl2br}}
- 1.1 添加了对变量过滤器
{{VAR|raw}}
{{VAR|html}}
{{VAR|js}}
的支持 - 1.0 初始版本