pytocryto / pytotpl
PHP 模板引擎
1.0.0
2018-04-26 19:37 UTC
Requires
- php: >=5.6.4
- evenement/evenement: 2.0.*
- symfony/debug: ~3.2
Requires (Dev)
- phpunit/phpunit: ~5.7
This package is not auto-updated.
Last update: 2024-09-20 19:40:21 UTC
README
安装
通过 composer 安装
composer require pytocryto/pytotpl
初始化与配置 PytoTPL
$tpl = new \PytoTPL\PytoTPL(); $tpl->setConfig([ 'tpl_folder' => __DIR__ . '/dir/to/templates/', 'cache_folder' => __DIR__ . '/dir/to/cache/', 'tpl_file_format' => '.tpl', 'compress' => false, // compression only available in a valid <HTML> document 'event_emitter' => true, // set to false for better performance ]);
输出
{$variable}
{$array.key1.key2.key3}
{echo $variable}
{print $variable}
{$variable|substr:0,-1}
未转义的输出
{!! $variable !!}
循环
{foreach: $myArray as $item}
..
{/foreach}
{for: $i = 0; $i < 99; $i++}
..
{/for}
{while: $i < 9}
..
{/while}
条件语句
{if: date('Y') == 2017}
Yey, its 2017!
{elseif: date('Y') == 2018}
Yey, its 2018!
{else}
NOOOOo :(
{/if}
PytoTPL 提供了一些快捷方式,而不是需要编写像这样的条件
{if: isset($data)}
Yes
{else}
Nope
{/if}
{-- or --}
{if: empty($data)}
...
{/if}
你将直接写
{isset: $data}
Yes
{else}
Nope
{/isset}
{-- or --}
{empty: $data}
...
{/empty}
条件
{continue}
{break}
而不是编写长条件,例如
{if: $userid == 1}
{continue}
{/if}
你可以简单地在单行中包含条件
{continue: $userid == 1}
{break: $userid == 1}
扩展/包含布局
{extend 'header'}
设置变量
注意:使用 PytoTPL 语法设置变量时,不需要以分号 ";" 结尾
{var $myVariable = 'My value here'}
{-- or --}
{assign $myVariable = 'My value here'}
使用三元运算符
{$_GET['id'] OR 'No id has been set!'}
注释
{-- Anything here is just a PHP comment --}
部分(尚未测试!)
注意:部分名称必须以字母字符书写。
{section: mySection}
...
{/section}
如果你想将变量传递给部分,你可以这样做
{section: mySection, varName=Value}
...
{/section}
你也可以使用以下语法覆盖部分
{section.overwrite: mySection}
...
{/section}
渲染部分
{section.view: mySection}