① 从配置文件中读取配置:
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();负责显示
<?php include("init.inc.php");//引入配置文件
$attr=array("a"=>"aa","b"=>"bb","c"=>"cc");//注册关联数组
$smarty->assign("attr",$attr);//注册数组
$smarty->assign("title","helloword");//注册字符串 $smarty->assign("test","thisfjasdjfajsdfjlasdjlfjlsdjfdsjlkfjskldj");//注册字符串
$smarty->assign("try1","this is a test");
$smarty->assign("money","美元"); $smarty->assign("bs",1); $r=new Ren();
$smarty->assign("ren",$r);//注册对象 调对象要用$ren->name include("DBDA.php");
$db=new DBDA();
$sql="select * from Nation";
$attrr=$db->Query($sql,1,"test2"); $smarty->assign("nation",$attrr); $smarty->display("0429test.html"); class Ren
{
public $name='张三';
}
2, 0429test.html 属性配置调用 调节器 foreach循环 if语句 保留函数 类中变量调用
<!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>
显示效果: