您的位置:首页 > 其它

2016/04/29 smarty模板 1, 初步 目标 : 变量 运算符 表达式 流程控制 函数

2016-04-29 20:22 591 查看
① 从配置文件中读取配置:

1,在模板页面加载配置文件 html页面 不是php页面
<{config_load file='fo.conf'}>

2,在需要用到配置的地方加
<{#size#}>

3, 如果配置文件分了块,要取某一块的配置 用到section

<{config_load file='aa.conf' section='aa'}>

② 调用变量调节器: |

capitalize 单词首字母大写<{$test|capitalize}

cat 连接字符串 <{$articleTitle|cat:" yesterday."}>

lower 小写 <{$articleTitle|lower}> 类如upper

truncate 截取 <{$articleTitle|truncate:30}>

③ 自定义变量调节器:

1,在自定义的插件目录下新建文件,注意命名规则 (plugins插件文件夹下)

modifier.mark.php

2,在以上文件里面新建方法: 注意命名规则

smarty_modifier_mark

该方法必须有一个参数,代表变量本身

例:

该方法必须有一个参数,这个参数代表变量本身

function smarty_modifier_mark($str)
{
return "<mark>{$str}</mark>";

}

操作练习 : 0429test.php 中需要用到 DBDA.php 连接数据库 最后输出民族下拉菜单

1, 0429test.php

$smarty->assign(); 负责分配变量 和 $smarty->display();负责显示

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><{$title}></title>
</head>
<body>

<{config_load file='fo.conf' section=bb}>
<div style="width:200px; height:200px; background-color:<{#bg#}>;font-size:<{#size#}>px">测试config</div>

<{config_load file='fo.conf' section=aa}>
<div style="width:200px; height:200px; background-color:<{#bg#}>;font-size:<{#size#}>px">测试config</div>
<hr>

<div style="width:200px; height:200px; background-color:<{$smarty.config.bg}>;font-size:<{#size#}>px">测试config</div>

<h1>测试页面</h1>
<div><{$attr["a"]}></div>
<div><{$attr["b"]}></div>
<div><{$attr["c"]}></div>

<div><{$attr.a}></div>

<div><{$test|substr:10}></div>
<div><{$test|mark}></div>
<div><{$try1|capitalize}></div>
<div><{$money|cat:"$"}></div>

<select>
<{foreach $attr as $k=>$v}>
<option><{$k}>=><{$v}></option>
<{/foreach}>
</select>

<select>
<{foreach $attr as $k=>$v}>
<option><{$v@index}></option>
<{/foreach}>
</select>

<select>
<{foreach $attr as $k=>$v}>
<option><{$v@iteration}></option>
<{/foreach}>
</select>

<{foreach $attr as $k=>$v}>
<{if $v@first}>
<div style='width:40px; height:40px; background-color:yellow'><{$k}>=><{$v}></div>
<{else}>
<div style='width:40px; height:40px; background-color:red'><{$k}>=><{$v}></div>
<{/if}>
<{/foreach}>

<div><{$ren->name}></div>
<div><{$smarty.now}></div>
<div><{$smarty.now|date_format}></div>
<div><{$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"}></div>

<{if $bs==1}>
<div>bs=1</div>
<{else}>
<div>bs无</div>

<{/if}>

<select>
<{foreach $nation as $vv}>
<option value='<{$vv[0]}>'><{$vv[1]}></option>
<{/foreach}>
</select>
</body>
</html>


View Code
显示效果:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: