① 从配置文件中读取配置:
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();负责显示
1 <?php 2 3 include("init.inc.php");//引入配置文件 4 $attr=array("a"=>"aa","b"=>"bb","c"=>"cc");//注册关联数组 5 $smarty->assign("attr",$attr);//注册数组 6 $smarty->assign("title","helloword");//注册字符串 7 8 $smarty->assign("test","thisfjasdjfajsdfjlasdjlfjlsdjfdsjlkfjskldj");//注册字符串 9 $smarty->assign("try1","this is a test"); 10 $smarty->assign("money","美元"); 11 12 $smarty->assign("bs",1); 13 14 $r=new Ren(); 15 $smarty->assign("ren",$r);//注册对象 调对象要用$ren->name 16 17 include("DBDA.php"); 18 $db=new DBDA(); 19 $sql="select * from Nation"; 20 $attrr=$db->Query($sql,1,"test2"); 21 22 $smarty->assign("nation",$attrr); 23 24 $smarty->display("0429test.html"); 25 26 class Ren 27 { 28 public $name=‘张三‘; 29 }
2, 0429test.html 属性配置调用 调节器 foreach循环 if语句 保留函数 类中变量调用
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title><{$title}></title> 6 </head> 7 <body> 8 9 <{config_load file=‘fo.conf‘ section=bb}> 10 <div style="width:200px; height:200px; background-color:<{#bg#}>;font-size:<{#size#}>px">测试config</div> 11 12 <{config_load file=‘fo.conf‘ section=aa}> 13 <div style="width:200px; height:200px; background-color:<{#bg#}>;font-size:<{#size#}>px">测试config</div> 14 <hr> 15 16 <div style="width:200px; height:200px; background-color:<{$smarty.config.bg}>;font-size:<{#size#}>px">测试config</div> 17 18 19 20 <h1>测试页面</h1> 21 <div><{$attr["a"]}></div> 22 <div><{$attr["b"]}></div> 23 <div><{$attr["c"]}></div> 24 25 <div><{$attr.a}></div> 26 27 <div><{$test|substr:10}></div> 28 <div><{$test|mark}></div> 29 <div><{$try1|capitalize}></div> 30 <div><{$money|cat:"$"}></div> 31 32 <select> 33 <{foreach $attr as $k=>$v}> 34 <option><{$k}>=><{$v}></option> 35 <{/foreach}> 36 </select> 37 38 <select> 39 <{foreach $attr as $k=>$v}> 40 <option><{$v@index}></option> 41 <{/foreach}> 42 </select> 43 44 <select> 45 <{foreach $attr as $k=>$v}> 46 <option><{$v@iteration}></option> 47 <{/foreach}> 48 </select> 49 50 51 <{foreach $attr as $k=>$v}> 52 <{if [email protected]}> 53 <div style=‘width:40px; height:40px; background-color:yellow‘><{$k}>=><{$v}></div> 54 <{else}> 55 <div style=‘width:40px; height:40px; background-color:red‘><{$k}>=><{$v}></div> 56 <{/if}> 57 <{/foreach}> 58 59 60 <div><{$ren->name}></div> 61 <div><{$smarty.now}></div> 62 <div><{$smarty.now|date_format}></div> 63 <div><{$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"}></div> 64 65 <{if $bs==1}> 66 <div>bs=1</div> 67 <{else}> 68 <div>bs无</div> 69 70 <{/if}> 71 72 <select> 73 <{foreach $nation as $vv}> 74 <option value=‘<{$vv[0]}>‘><{$vv[1]}></option> 75 <{/foreach}> 76 </select> 77 </body> 78 </html>
显示效果: