corex/template

此包已被 废弃 并不再维护。未建议替代包。

简单的模板系统

2.1.0 2019-07-11 08:11 UTC

This package is auto-updated.

Last update: 2023-09-16 10:34:22 UTC


README

License Build Status codecov

此包的目标是简洁。它基于 Mustache,但采用非常简单的方法。如果您需要比这种“简洁”更多,建议使用 Mustache。

每次调用 load() 和 render() 都会使用基本路径。

Mustache 默认设置为转义变量。但是,在本包中已禁用。如果您需要转义值(例如 HTML 标签),可以调用模板上的 escape() 方法。

注意:当打印布局/视图 (__toString) 并抛出异常时,它将被捕获并通过 error_log() 发送。

// Set base path (can be called more than once). Paths will be searched in reverse order.
Template::basePath('/path/to/templates');
Template::basePath('/path/to/other/templates');
// Load template.
$template = Template::load('test');

// Set escape on variables (default un-escaped).
$template->escape();

// Set path for templates.
$template->path('/path/to/some/other/templates');

// Set variable on template.
$template->variable('myVar', 'myValue');
$template->var('myVar2', 'myValue2');

// Set variables.
$template->variables([
    'myVar1' => 'myValue1',
    'myVar2' => 'myValue2'
]);
$template->vars([
    'myVar3' => 'myValue3',
    'myVar4' => 'myValue4'
]);

// Render template.
$content = $template->render();
// Example of loading template, set var and render.
$content = Template::load('base')
    ->variable('myVar', 'myValue')
    ->var('myVar2', 'myValue2')
    ->render();
// Render template directly.
$content = Template::render('base', [
    'myVar1' => 'myValue1',
    'myVar2' => 'myValue2'
]);
// Parse template and render values.
$content = Template::parse('({{myVar}})', ['myVar' => 'myValue']);
// Get Mustache engine.
$mustacheEngine = Template::mustacheEngine();

有关 Mustache 的更多信息,请参阅 https://github.com/bobthecow/mustache.php/wiki