PHP code example of windwork / template
1. Go to this page and download the library: Download windwork/template library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
windwork / template example snippets
$this->view()->assign(‘变量’, “值”); // 变量名为字符串,值为任意数据类型。
$this->view()->render($tpl = ‘模板文件’, $spare = "备选模板文件");
// tpl1.html 不存在的时候,使用tpl2.html,tpl2.html也不存在则报tpl1.html不存在的异常
$this->view()->render(‘tpl1.html’, "tpl2.html");
// 默认 $tpl = "{$mod}/{$ctl}.{$act}.html"
$this->view()->render();
class AccountController extends \wf\app\web\Controller {
public function loginAction() {
// 模板变量赋值
$this->view()->assign('time', time());
// 显示模板 app/user/view/account.login.html
$this->view()->render();
// 显示模板 app/user/view/account.login.m.html
$this->view()->render('user/account.login.m.html');
}
}
{loop $arr $var}
...
{/loop}
解析后为
foreach($arr as $var) :
{if $a}
{elseif $b}
{else}
{/if}
{for $a = 0; $a < $x; $a++}
{/for}
<!--{模板标签}-->
模板解析时将去掉模板标签两边的html注释变成
{模板标签}
// 模板可设置参数
$tplOpt = [
// 模板目录,相对于入口文件所在目录
'tplDir' => 'template/default',
// 渲染模板时,是否检查模板文件是否需要编译
'compileCheck' => true,
// 设置模板识别id,用于区分不同国家的语言,不同用户使用不同模板等
'compileId' => 'zh_CN',
// 编译后的模板文件保存的文件夹
'compileDir' => 'data/template',
// 是否强制每次都编译
'compileForce' => false,
// 编译后的模板文件是否合并成一个文件
'compileMerge' => true,
// 默认模板文件,建议是"{$mod}/{$ctl}.{$act}.html"
'defaultTpl' => '',
// 默认备用模板文件,为空或跟默认模板文件一样,则不使用备用模板文件,建议是"{$mod}/{$ctl}.{$act}.html"
'defaultSpareTpl' => '',
];
$view = new \wf\template\adapter\Wind($tplOpt);
// 变量赋值
$view->assign('myVar', '123456');
// 使用模板 template/default/my/demo.html
$view->render('my/demo.html');
echo $var;
code...