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

这篇文章主要介绍了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. }
  13. {/literal}
  14. </script>
  15. <title>{#pageTitle#}</title>
  16. <body bgcolor="{#bodyBgColor#}">
  17. <table bgcolor="{#tableBgColor#}" >
  18. <tr bgcolor="{#rowBgColor#}">
  19. <td>dosomething</td>
  20. <td>帅锅</td>
  21. </tr>
  22. </table>
  23. {$smarty.server.SERVER_NAME}
  24. <hr />
  25. {$str|count_words}
  26. <hr />
  27. -------常量--------<br>
  28. {$smarty.now}<br />{$smarty.const.MY_CONST}<br />{$smarty.template}<br />{$smarty.current_dir}<br />{$smarty.version}<br />{$smarty.ldelim|cat:$smarty.rdelim}
  29. <hr />
  30. {$smarty.now|date_format:$config}
  31. {$yesterday|date_format:'Y-m-d'}
  32. <hr />
  33. {$string|default:'default变量修饰:smarty学习'}
  34. <hr />
  35. {$str1|escape:'html'}<br />{$str2|escape:'mail'}
  36. <hr />
  37. <p>{$str1|indent|upper}</p>
  38. {$str1|nl2br}
  39. <hr />
  40. {$str1|regex_replace:"/@\d{3}/":"ABC"}<br />
  41. {$str1|replace:"163":"sina"}<br />
  42. {$str1|spacify}<br />
  43. <hr />
  44. {$number|string_format:"%.2f"}<br />
  45. {$number|string_format:"%d"}<br />
  46. <hr />
  47. {$str3|strip:"|"}<br />
  48. 去除包含在<>之间的字符:{$str3|strip_tags}<br />
  49. 截取长度:{$str3|truncate:10:"...":true}<br />
  50. 按长度换行:{$str3|wordwrap:30:"<br />"}
  51. <hr />
  52. {append var='name' value="Bob" index="first"}
  53. {append var='name' value="John" index="last"}
  54. {$name.last}<br />
  55. {foreach from=$family item=home}
  56. {foreach from=$home item=person}
  57. {$person}
  58. {/foreach}
  59. {/foreach}
  60. {$family[1].girl}
  61. <hr />
  62. {assign var="name" value="张三丰"}
  63. {$name}
  64. </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. }
  17. $dog = new Dog();
  18. $dog->name="小狗";
  19. $smarty->assign("dog",$dog);
  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. $config = "Y-m-d H:i:s";
  30. $smarty->assign("config",$config);
  31. $smarty->assign("yesterday",strtotime('-1 day'));
  32. //常量
  33. define("MY_CONST","百度");
  34. //append成员方法的使用
  35. $family = array("husband"=>"帅锅","wife"=>"美女");
  36. $famiadd = array("boy"=>"张三丰","girl"=>"王昭君");
  37. $smarty->append("family",$family);
  38. $smarty->append("family",$famiadd);
  39. echo "<pre>";
  40. print_r($family);
  41. $smarty->display("temp.htm");
  42. ?>