当前位置:首页 > PHP教程 > Smarty > 列表

smarty模板引擎中变量及变量修饰器用法实例

发布:smiling 来源: PHP粉丝网  添加日期:2021-05-08 21:50:10 浏览: 评论:0 

这篇文章主要介绍了smarty模板引擎中变量及变量修饰器用法,实例分析了smarty模板引擎中常见变量、变量修饰器及常量的具体用法,需要的朋友可以参考下

本文实例讲述了smarty变量及变量修饰器的应用。分享给大家供大家参考。具体如下:

模板文件:temp.htm:

  1. {config_load file="foo.conf"
  2. {$name.na1|cat:$name['na2']} 
  3. {$name['na1']|cat:'与'|cat:$name.na2} 
  4. {foreach from=$name item=na
  5. {$na} 
  6. {/foreach} 
  7. {$dog->leee()}{$dog->name} 
  8. <script> 
  9. {literal} 
  10. function foobar{ 
  11.  alert('foobar!'); 
  12. {/literal} 
  13. </script> 
  14. <title>{#pageTitle#}</title> 
  15. <body bgcolor="{#bodyBgColor#}"> 
  16. <table border="{#tableBorderSize#}" bgcolor="{#tableBgColor#}" > 
  17.  <tr bgcolor="{#rowBgColor#}"> 
  18.   <td>dosomething</td> 
  19.   <td>帅锅</td> 
  20.  </tr> 
  21. </table> 
  22. {$smarty.server.SERVER_NAME} 
  23. <hr /> 
  24. {$str|count_words} 
  25. <hr /> 
  26. -------常量--------<br> 
  27. {$smarty.now}<br />{$smarty.const.MY_CONST}<br />{$smarty.template}<br />{$smarty.current_dir}<br />{$smarty.version}<br />{$smarty.ldelim|cat:$smarty.rdelim} 
  28. <hr /> 
  29. {$smarty.now|date_format:$config} 
  30. {$yesterday|date_format:'Y-m-d'} 
  31. <hr /> 
  32. {$string|default:'default变量修饰:smarty学习'} 
  33. <hr /> 
  34. {$str1|escape:'html'}<br />{$str2|escape:'mail'} 
  35. <hr /> 
  36. <p>{$str1|indent|upper}</p> 
  37. {$str1|nl2br} 
  38. <hr /> 
  39. {$str1|regex_replace:"/@\d{3}/":"ABC"}<br /> 
  40. {$str1|replace:"163":"sina"}<br /> 
  41. {$str1|spacify}<br /> 
  42. <hr /> 
  43. {$number|string_format:"%.2f"}<br /> 
  44. {$number|string_format:"%d"}<br /> 
  45. <hr /> 
  46. {$str3|strip:"|"}<br /> 
  47. 去除包含在<>之间的字符:{$str3|strip_tags}<br /> 
  48. 截取长度:{$str3|truncate:10:"...":true}<br /> 
  49. 按长度换行:{$str3|wordwrap:30:"<br />"} 
  50. <hr /> 
  51. {append var='name' value="Bob" index="first"
  52. {append var='name' value="John" index="last"
  53. {$name.last}<br /> 
  54. {foreach from=$family item=home
  55. {foreach from=$home item=person
  56. {$person} 
  57. {/foreach} 
  58. {/foreach} 
  59. {$family[1].girl} 
  60. <hr /> 
  61. {assign var="name" value="张三丰"
  62. {$name} 
  63. </body> 

php文件:index.php

  1. <?php 
  2. require_once('libs/Smarty.class.php'); 
  3. $smarty = new Smarty(); 
  4. $smarty->setTemplateDir($_SERVER['DOCUMENT_ROOT']."/php/templates/"); 
  5. $smarty->setCompileDir($_SERVER['DOCUMENT_ROOT']."/php/templates_c/"); 
  6. $smarty->setCacheDir($_SERVER['DOCUMENT_ROOT']."/php/cache/"); 
  7. $smarty->caching = false; 
  8. $arr = array("na1"=>"帅锅","na2"=>"美女"); 
  9. $smarty->assign("name",$arr); 
  10. class Dog{ 
  11.  public $name
  12.  public $age
  13.  function leee(){ 
  14.   return $this->name."在干吗"
  15.  } 
  16. $dog = new Dog(); 
  17. $dog->name="小狗"
  18. $smarty->assign("dog",$dog); 
  19.  
  20. //$str = "hello world,i am here. i love smarty!"; 
  21. $str = "帅锅"
  22. $str1 = "<a href='http://www.sina.com/'>新浪</a> And\n aassu@163.com"
  23. $str2 = "aassu@163.com"
  24. $smarty->assign("str",$str); 
  25. $smarty->assign("str1",$str1); 
  26. $smarty->assign("str2",$str2); 
  27. $smarty->assign("number",30.293934); 
  28. $smarty->assign("str3","akie abfal   <a;fa>,dooerw,show databases,desc table"); 
  29.  
  30. $config = "Y-m-d H:i:s"
  31. $smarty->assign("config",$config); 
  32. $smarty->assign("yesterday",strtotime('-1 day')); 
  33. //常量 
  34. define("MY_CONST","百度"); 
  35.  
  36. //append成员方法的使用 
  37. $family = array("husband"=>"帅锅","wife"=>"美女"); 
  38. $famiadd = array("boy"=>"张三丰","girl"=>"王昭君"); 
  39. $smarty->append("family",$family); 
  40. $smarty->append("family",$famiadd); 
  41. echo "<pre>"
  42. print_r($family); 
  43. $smarty->display("temp.htm"); 
  44. ?>

Tags: smarty变量 smarty修饰器

分享到: