php学习——smarty

本文详细介绍了Smarty模板引擎的配置步骤及使用方法,包括变量赋值、模板显示、配置文件加载与使用等核心功能,并提供了丰富的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

smary文件配置步骤

//步骤1
require '/libs/Smarty.class.php';
//步骤2
$smarty = new Smarty;
//步骤3
/*
$smarty->template_dir = "";
$smarty->compile_dir = "";
$smarty->cache_dir = "";
$smarty->caching = 1;
$smarty->cache_lifetime = 60*60;//秒,这里的值是一个小时
$smarty->left_delimiter ='{#';
$smarty->right_delimiter = '#}';
$smarty->compile_check = true;
$smarty->debugging = true;
*/
//步骤4
$smarty->assign("demo1", "fengsulei");
$smarty->assign("demo2", "cool is working");
$smarty->assign(array("demo3"=>"string1","demo4"=>"smarty is worked"));
//assign 赋值,有两种形式
//$smarty->assign("demo1", "fengsulei");
//$smarty->assign("demo1", $str);
//$smarty->assign(array("city"=>"string1","state"=>"string2"));
$smarty->assign("p1","kloyu");
$farray=array("a","b","c","d","e","ss"=>99);
$smarty->assign("farray",$farray);
//步骤5
$smarty->display('index.tpl');
//显示有三种方式
//使用文件$smarty->display('index.tpl')
//使用路径$smarty->display('admin/index.tpl')
//使用绝对路径$smarty->display('www/templates/index.tpl'),$smarty->display('file:D:/wamp/www/templates/index.tpl')

 模板代码示例

<h1>{#$demo1#}</h1>
<b>{#$demo2#}</b>
<A HREF="#">{#$demo3#}</A>
<A HREF="#">{#$demo4#}</A>
{#$p1|upper|truncate:4#}
get
{#$smarty.get.att#}
post
{#$smarty.post.att#}
cookies
{#$smarty.cookies.att#}
session
{#$smarty.session.att#}
request
{#$smarty.request.att#}
server_name
{#$smarty.server.SERVER_NAME#}
env.path
{#$smarty.env.PATH#}

<p class="STYLE1">foreach exp1

</p>
<p>{#foreach from=$farray item=row#}
{#$row#}<br />
{#foreachelse#}
$farray is empty
{#/foreach#}</p>
<p class="STYLE1"> foreach exp2</p>
<p>{#foreach key=kk item=vv from=$farray #}
{#$kk#}-{#$vv#}<br />
{#foreachelse#}
$farray is empty
{#/foreach#}
</p>

 

<p>{#section name=line loop=$farray #}
{#$farray[line]#}<br />
{#if $smarty.section.line.iteration==5#}
{#$farray.ss#}<br />
{#/if#}
{#sectionelse#}
$farray is empty
{#/section#} </p>

 

配置文件实例

#global variable

page_titile="xxxxxx"
body_color="#333"
table_bgcolor="#666"

[index]
index_page_titile=""
index_body_color=""
index_table_bgcolor=""

[list]
list_page_titile=""
list_body_color=""
list_table_bgcolor=""

[view]
view_page_titile=""
view_body_color=""
view_table_bgcolor=""

加载配置文件

{config_load file="head.conf" section|scope|global=""}

config_load 参数
  类型默认值
section加载配置文件特定的一节string
scope加载数据的作用域,取值是local,parent,global。local指定变量作用域是当前模板,parent指定变量作用域是当前模板和当前模板的父模板stringlocal
global加载变量是否全局可见boolfalse

代码示例

{#config_load file="head.conf"#}

引用配置文件代码示例

1,{#$smarty.config.page_titile#}

2,{##page_titile##}

模板包含

{include file="模板文件名" assign="指定传递给的变量名(变量名不加$符号)"}

常用变量

capitalize所有字符首字符大写
count_characters计算字符个数
cat将cat里的参数值连接到给定变量后
count_paragraphs计算段落数量
count_sentences计算句子数量
count_words计算单词数量
date_format时间格式化
default给空变量设置一个默认值
escape用于html转码
indent每行缩进字符个数
lower变量小写
nl2br换行符转换成<br />
regex_replace使用正则替换
replace替换和搜索
spacify在字符之间插入字符,默认插入空格
string_format 
strip替换所有重复空格,换行,tab为指定字符
strip_tags去除html标签
truncate截取字符串
upper变量大写
wordwrap指定段落宽度,默认80个字符

foreach循环参数和设置

foreach
form使用数组array 
item当前处理元素的值string 
key当前处理元素的键名string 
name循环的名称string 
    
foreach 可调用变量
iteration循环当前执行次数
first是否是循环第一次执行,是则返回true
last是否是循环最后一次执行,是则返回true
show 
total循环执行总数

section循环参数和变量

section
name指定循环变量名  
loop使用数组名  
start从第几次开始  
setp每次增加值  
max最多循环次数  
show是否显示该循环值为bool型  
section 可调用的变量
index显示当前索引,如果指定了start则从该设置值开始每次加1,如果设置了setp则按设置值增加
index_prev显示上次循环的索引值,首次值为-1
index_next显示下一次循环索引值
iteration显示循环次数
first判断是否是循环执行的首次,是则返回true
last判断是否是循环执行的最后一次,是则返回true
rownum同iteration,互为别名
loop显示该循环上次的索引
show 
total显示循环执行总次数

转载于:https://www.cnblogs.com/fslnet/archive/2012/04/08/2438216.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值