Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  php  >  正文 有关smarty的基本设置

有关smarty的基本设置

发布时间:2015-01-19   编辑:www.jquerycn.cn
有关smarty的基本设置,以实例的方式,告诉大家常用的方法。

有关smarty的基本设置,以实例的方式,告诉大家常用的方法。
1、将lib文件夹导入项目中,改名字为smarty
2、编写配置程序,并建立相应的模板、编译、缓存文件夹
smarty_inc.php
 

复制代码 代码如下:
<?php
include_once("smarty/Smarty.class.php");
$smarty=new 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";
$smarty->left_delimiter="{";
$smarty->right_delimiter="}";
?>

3、编写php文件
index.php
 

复制代码 代码如下:
<?php
include("smarty_inc.php");
$name[]=array("name"=>"新闻第一条","date"=>"2008-1-1");
$name[]=array("name"=>"新闻第二条","date"=>"2008-2-1");
$name[]=array("name"=>"新闻第三条","date"=>"2008-3-1");
$row=array("标题","作者");
$smarty->assign("title",$name);
$smarty->assign("row",$row);
$smarty->display("index.html")
?>

4、在templates模板文件夹中编写index.html
index.html
 

复制代码 代码如下:
<html>
{$row[0]}|{$row[1]}
{section name=list loop=$title}
<b><font color="red">
{$title[list].name}--{$title[list].date}
</font></b><br>
{/section}
</html>
 


5、使用变量操作符
index.php
 

复制代码 代码如下:
<?php
include("smarty_inc.php");
$value="it is Work and, it Is video";
$smarty->assign("name",$value);
$smarty->display("index.html")
?>
 

index.html
 

复制代码 代码如下:
<html>
原始内容 : {$name}<br>
{$name|capitalize}<br>
{$name|cat:"演示"}<br>
{$smarty.now|date_format:'%Y-%M-%D'};
{$name|repalce:"is":"***"};
{$name|truncate}
</html>

6、foreach
index.php
 

复制代码 代码如下:
<?php
include("smarty_inc.php");
$value=array(4,5,6,7);
$value_key=array('a'=>"PHP",'b'=>"JAVA",'c'=>"C++");
$smarty->assign("name",$value);
$smarty->assign("name_key",$value_key);
$smarty->display("index.html")
?>
 

index.html
 

复制代码 代码如下:
<html>
{include file="header.html"}
{foreach from=$name item=id}
数组内容: {$id}<br>
{/foreach}
{foreach from=$name item=id key=k}
数组内容: {$k}--{$id}<br>
{/foreach}
</html>

7、literal
当出现大括号,如使用javascript,可以用literal进行文本处理

8、strip
优化页面,使标签中的空格去掉。使 人不轻易盗用
 
9、缓存
基本配置:
 

复制代码 代码如下:
<?php
include_once("smarty/Smarty.class.php");
$smarty=new Smarty();
$smarty->config_dir="smarty/Config_File.class.php";
$smarty->caching=true;
$smarty->template_dir="./templates";
$smarty->compile_dir="./templates_c";
$smarty->cache_dir="./smarty_cache";
$smarty->cache_lifetime=60;
$smarty->left_delimiter="{";
$smarty->right_delimiter="}";
?>

带ID的缓存
 

复制代码 代码如下:
<?php
include("smarty_inc.php");
$id=$_GET[id];
$value=array(4,5,6,7);
$smarty->assign("name",$value);
$smarty->assign("id",$id);
$smarty->display("index.html",$id);
?>

清除缓存和局部缓存
 

复制代码 代码如下:
<?php
include("smarty_inc.php");
$id=$_GET[id];
$value=array(4,5,6,7);
//使用insert的局部缓存:
function insert_shijian(){
return date("Y-m-d H:m:s");
}
$smarty->assign("name",$value);
$smarty->assign("id",$id);
$smarty->display("index.html",$id);
//删除带ID的缓存$smarty->clear_cache('index.html',$id);
//删除全部缓存$smarty->clear_all_cache();
?>
复制代码 代码如下:

<html>
{insert name='shijian'}
</html>

您可能感兴趣的文章:
有关smarty的基本设置
smarty获得当前url示例代码
PHP模板引擎Smarty缓存使用
smarty缓存应用与清除
smarty 模板if else使用实例与入门教程
php smarty 基础
在php中配置使用smarty模板引擎
有关smarty缓存的应用
有关在smarty中调用php内置函数的问题
smarty结合xajax检测用户名

[关闭]