<?php
header("content-type:text/html;charset=utf-8");
//引入核心文件并实例化
require ('/WWW/testSmarty/smarty/Smarty.class.php');
$smarty = new Smarty();
//配置
//模板文件的路径
$smarty->template_dir = '/WWW/testSmarty/test/tpl';
//模板文件编译后得到的文件的路径
$smarty->compile_dir = '/WWW/testSmarty/test/template_c';
//缓冲文件的路径
$smarty->cache_dir = '/WWW/testSmarty/test/cache';
//开启缓冲,缓冲默认是关闭的
$smarty->caching = true;
//缓冲的保留时间
$smarty->cache_lifetime = 120;
?>
<?php
//引入配置文件
require ('config.php');
//向模板文件中传递值
$smarty->assign('text','hello Smarty');
//渲染模板
$smarty->display('start.tpl');
?>
//向要渲染的模板传递值
$smarty->assign('变量名',值)
{if $variable 条件修饰符 value1}
//some
{elseif $variable 条件修饰符 value2}
//some
{else}
//some
{/if}
<?php
require ('config.php');
$article = array(
array(
'title'=>'第一章',
'name'=>'QxQstar',
'content'=>'Smarty section循环第一章'
),
array(
'title'=>'第二章',
'name'=>'QxQstar',
'content'=>'Smarty section循环第二章'
)
);
$smarty->assign('article',$article);
$smarty->display('section.tpl');
?>
<ul>
{section name=item loop=$article}
<li>
{$article[item].title}
<p>作者:{$article[item].name}</p>
<p>内容:{$article[item].content}</p>
</li>
{sectionelse}
没有文章
{/section}
</ul>
{foreach $arrayvar as $itemvar}
{foreach $arrayvar as $keyvar=>$itemvar}
<?php
require ('config.php');
$articles = array(
array(
'title'=>'第一章',
'name'=>'QxQstar',
'content'=>'Smarty foreach循环第一章'
),
array(
'title'=>'第二章',
'name'=>'QxQstar',
'content'=>'Smarty foreach循环第二章'
)
);
$smarty->assign('articles',$articles);
$smarty->display('foreach.tpl');
?>
<ul>
{foreach $articles as $article}
<li>
{$article.title}
<p>{$article.name}</p>
<p>{$article.content}</p>
</li>
{foreachelse}
<p>没有文章</p>
{/foreach}
</ul>
{for $i = 0 to 10 step 2}
{$i}
{foreach}
不能进行循环
{/for}
{while $variable > 0}
//some
{/while}
{blockName param1 = value...}
{$content}
{/blockName}
//function插件的使用
{include file='header.tpl'}
//modifier插件的使用
{'abc'|cat:'d'}
//block插件的使用
{textformat wrap = 10}
abc def
ghikj lmn
{/textformat}
<?php
require ('config.php');
function test($params){
$p1 = $params['p1'];
$p2 = $params['p2'];
return $p1.$p2;
}
$smarty->registerPlugin('function','f_test','test');
$smarty->display('definedFunc.tpl');
?>
function smarty_function_test($params){
//$params是一个关联数组
$w = $params['w'];
$h = $params['h'];
return $w * $h;
}
?>
function smarty_modifier_test2($utime,$formate){
return date($formate,$utime);
}
function smarty_block_test3($params ,$content){
$replace = $params['replace'];
$maxnum = $params['maxnum'];
if($replace == 'true'){
str_replace(',', ',', $content);
str_replace('。', '.', $content);
}
$content = substr($content,0,$maxnum);
return $content;
}
{test3 replace='true' maxnum = 6}
abcdfedddderere
{/test3}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有