PHP code example of digphp / template
1. Go to this page and download the library: Download digphp/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/ */
digphp / template example snippets php
$template = new \DigPHP\Template\Template('可以传入一个 Psr\SimpleCache\CacheInterface 实例,传入后会开启模板缓存,提高效率');
// 添加分区
$template->addPath('book','/www/template/book');
$template->addPath('cms','/www/template/cms', 1);
$template->addPath('cms','/www/template/cms2', 2);
// 声明变量
$template->assign('name','小刘');
$template->assign('sex','男');
$template->assign([
'name'=>'小刘',
'sex'=>'男'
]);
// 渲染
$template->renderFromFile('content@cms'); // 渲染cms分区下的content模板 系统会根据权重顺序在添加的分区目录中依次寻找content模板
$template->renderFromFile('index@book'); // 渲染book分区下的index模板
// 直接渲染模板字符串
$template->renderFromString('hello {$name}', ['name'=>'隔壁小王']); // 直接渲染字符串模板
// 语法扩展
// 定一个{datetime},渲染成当前时间
$template->extend('/\{datetime\}/Ui', function ($matchs) {
return ' echo date(\'Y-m-d H:i:s\'); php
{$name}
{$vo.title}
{$vo['title']}
// 原始输出
{echo $name}
{echo $vo['title']}
{php echo $vo['title']}
php
{:mb_substr($title, 0, 80)}
{:date('Y-m-d', $time)}
// 原始输出
{echo date('Y-m-d', time())}
{php echo date('Y-m-d', time())}
{php print_r($list)}
html
// 定义函数
{function somefunc($a, $b)}
<code>{$a}</code>与<code>{$b}</code>{$b}求和,其值是<code>{echo $a+$b}<code>
{/function}
// 执行函数
{echo somefunc(1,2)}
php
// 方式一
echo $title;
php
{foreach $list as $key=>$vo}
{$key}:{$vo}
{/foreach}
php
{for $i=0; $i < 10; $i++}
{$i}
{/for}
php
{if isset($a)}
// somecode
{elseif $b==1}
// somecode
{else}
// somecode
{/if}
php
{switch $a}
{case 1}
// some..
{/case}
{case 2}
// some..
{/case}
{default}
// some..
{/default}
{/switch}