频道栏目
首页 > 资讯 > Smarty > 正文

Smarty配置与基本操作

16-01-08        来源:[db:作者]  
收藏   我要投稿
include_once("Smarty/Smarty.class.php");//包含smarty类文件
   
$smarty = new Smarty(); //建立smarty实例对象$smarty
   
$smarty->config_dir="Smarty/Config_File.class.php";  // 目录变量
   
$smarty->caching=false; //是否使用缓存,项目在调试期间,不建议启用缓存
   
$smarty->template_dir = "./templates";//设置模板目录
   
$smarty->compile_dir ="./templates_c"; //设置编译目录
   
$smarty->cache_dir ="./smarty_cache"; //缓存文件夹
   
//----------------------------------------------------
   
//左右边界符,默认为{},但实际应用当中容易与JavaScript相冲突
   
//----------------------------------------------------
   
$smarty->left_delimiter = "{";
   
$smarty->right_delimiter ="}";
PHP模板引擎Smarty的变量操作符

1.摸版接收数组显示

{section name=i loop=$content}

{$content[i].name}

{sectionelse}

no

{/section}

2.如何使用Smarty变量操作符

语法中使用"|"应用变量操作符,多个参数用":" 分隔开来

capitalize [首字母大写] e.g:{$content|capitalize}

count_characters [计算字符数] e.g:{$content|count_characters}

cat [连接字符串] e.g:{$content|cat:$title} :号连接

count_paragraphs [计算段落数] e.g:{$content|count_paragraphs }

count_sentences [计算句数] e.g:{$content|count_sentences}

count_words [计算词数] e.g:{$content|count_words}

date_format [时间格式] e.g:{$content|date_format:"%Y-%m-%d"}

default [默认] e.g:{$content|default:"no content}

escape [转码] e.g:{$content|escape}

indent[缩进] e.g:{$content|indent:10:' '}缩进10个字符,以空格代替

lower[小写 ] e.g:{$content|lower}

nl2br[换行符替换成
]

regex_replace[正则替换]

replace[替换] e.g:{$content2|replace:"as":"***"}

spacify[插空] e.g:{$content2|spacify}

string_format[字符串格式化] e.g:{$content2|string_format:"%.2f"}

strip[去除(多余空格)] e.g:{$content2|strip}

strip_tags[去除html标签] e.g:{$content2|strip_tags}

truncate[截取] e.g:{$content2|truncate:"30":"…"}

upper[大写]

wordwrap[行宽约束]

PHP模板引擎Smarty内置函数

1.foreach数组的处理

2、include多功能使用

3、IF条件语句的使用

4、literal strip文本的处理技巧

1.foreach数组的处理

与我们在PHP中的foreach用法类似,同样是用来遍历数组。在实际操作中与

section 功能一样,但foreach在Smarty一般用来处理一维数组。

=======无键值数组
{foreach from=$name item=id}
id: {$id}
 
{/foreach}
=======有键值数组
{foreach key=j item=v from=$name }
{$j}: {$v}
 
{/foreach}

2、include多功能使用

{include file="header.htm"}
{include file="D:\www\head.htm"}
{include file='head.htm' title="MainMenu"}

3、IF条件语句的使用

{if $name=='ok'}
{else}
{/if}

4、literal strip文本的处理技巧

literal数据将被当作文本处理,此时模板将忽略其内部的所有字符信息. 该特性用

于显示有可能包含大括号等字符信息的 javascript 脚本

{literal}
<script language=javascript>
</script>
{/literal}

strip标记中数据的首尾空格和回车. 这样可以保证模板容易理解且不用担心多余的

空格导致问题.{strip}{/strip}

PHP模板引擎Smarty缓存应用

1、Smarty缓存的配置

2、Smarty缓存的使用和清除

3、Smarty局部缓存

4、MYSQL与Smarty的应用

1、Smarty缓存的配置

$smarty->cache_dir ="/caches/";  //缓存目录
$smarty->caching = true;  //开启缓存,为flase的时侯缓存无效
$smarty->cache_lifetime = 60;  //缓存时间

2、Smarty缓存的使用和清除

$smarty->display('cache.tpl',cache_id);  //创建带ID的缓存
$smarty->clear_all_cache(); //清除所有缓存
$smarty->clear_cache('index.htm'); //清除index.tpl的缓存
$smarty->clear_cache('index.htm',cache_id);//清除指定id的缓存

3、Smarty局部缓存

insert函数默认是不缓存的。并且这个属性不能修改

index.htm
{insertname="get_time"}
index.php function insert_get_time(){ return date("Y-m-d H:m:s"); }

smarty_block函数功能更加强大,使用方法同上

{blockname}
没有缓存的:{$smarty.now}
{/blockname}

 


                
相关TAG标签
上一篇:程序中通过define定义的常量如何在smarty模板中使用
下一篇:详解PHP的Smarty缓存操作(2)
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站