php学习——smarty

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指定变量作用域是当前模板和当前模板的父模板 string local
global 加载变量是否全局可见 bool false

代码示例

{#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/p/2438216.html