Smarty模板中变量修饰格式: {$var|方法1:参数1:参数2…|方法2…}

来源:互联网 发布:表扬淘宝客服的话语 编辑:程序博客网 时间:2024/06/03 19:23
capitalize [首字母大写] 
cat :”characters”[连接字符串] 
count_characters:true/false [计算字符数] 
count_paragraphs [计算段落数]
count_sentences [计算句数]
count_words [计算词数]
date_format:”%Y-%m-%d” [时间格式]
Default:”aaa” [默认]
indent[缩进]
lower[小写 ]
nl2br[换行符替换成<br />]
regex_replace[正则替换]
replace[替换]
string_format[字符串格式化]
strip[去除(多余空格)]
strip_tags[去除html标签]
truncate[截取]
upper[大写]
原文:{$str}
<p>
变量中的字符数(包括空格):{$str|count_characters:true}
使用变量修饰方法后:{$str|nl2br|upper|cat:"this is cat test"}<br>
data:{$smarty.now|date_format:"%Y-%m-%d"}Smarty 中的 if 语句和 php 中的 if 语句一样灵活易用,并增加了几个特性以适宜模板引擎. 
if 必须于 /if 成对出现. 可以使用 else 和 elseif 子句. 
可以使用以下条件修饰词:eq、ne、neq、gt、lt、lte、le、gte、ge、is even、is odd、is not even、is not odd、not、mod、div by、even by、odd by、==、!=、>、<、<=、>=. 使用这些修饰词时必须和变量或常量用空格格开.

  Section一般用于比较复杂的数组{section name=secname loop=$arr start=num step=num}
{/section}
Name是循环名称,loop为循环数组,start为循环的起始位置,step是循环步长。
<table width="500" border="1">
  <tr>
    <td width="187">用户名</td>
    <td width="254">性别</td>
    <td width="37">电话</td>
  </tr>
  {section name=sec1 loop=$rsarr} 
  <tr>
     <td>{$rsarr[sec1].name}</td>
    <td>{$rsarr[sec1].sex}</td>
    <td>{$rsarr[sec1].tel}</td>
  </tr>
  {/section}
</table>

Literal 标签区域内的数据将被当作文本处理,此时模板将忽略其内部的所有字符信息. 该特性用于显示有可能包含大括号等字符信息的 javascript 脚本. 当这些信息处于 literal  /literal标签中时,模板引擎将不分析它们,而直接显示.
{literal} 
<script language=javascript> 
<!– 
function say(field) { 
alert(“hello”);
}
 // --> </script>
 {/literal}

Smarty的变量
$template_dir
$compile_dir
$cache_dir
$config_dir
$debugging
$caching
使用缓存:
$smarty->cache_dir = "cache";
$smarty->cache_lifetime = 60 * 30; 
$smarty->caching = true; 
// 模板缓存
if(!$smarty->is_cached("tpl_cache.tpl")) {
    // do database calls, assign vars here
}
$smarty->display("tpl_cache.tpl");


设置单个缓存会话期
$smarty->cache_dir = "cache";
$smarty->caching = 2; 
// set the cache_lifetime for index.tpl to 5 minutes 
$smarty->cache_lifetime = 300; 
$smarty->display('index.tpl'); 
// set the cache_lifetime for home.tpl to 1 hour 
$smarty->cache_lifetime = 3600; 
$smarty->display('home.tpl');

清除缓存
$smarty->cache_dir = "cache";
$smarty->caching = true;
// clear out all cache files 
$smarty->clear_all_cache(); 
// clear only cache for index.tpl 
$smarty->clear_cache('index.tpl'); 
$smarty->display('index.tpl');

Fetch方法
$smarty->assign('title','第一个Smarty程序');
$smarty->assign('content','Hello,Welcome to study \'Smarty\'!');
//$smarty->output('a.html');
$output=$smarty->fetch('index.tpl');
$fp=fopen("a.html","wb");
fwrite($fp,$output);
fclose($fp);
0 0