smarty post实例

知道了smarty的运行原理之后,就想写个post的实现过程,以下是实现post的代码,跟独自写php代码差不多,只是多了显示文件

配置:conf.php

  1. <?php
  2. @header("Content-type: text/html; charset=UTF-8");
  3. require '../libs/Smarty.class.php';
  4. $my=new Smarty;
  5. $my->template_dir ='templates/';
  6. $my->compile_dir ='templates_c/';
  7. $my->config_dir ='configs/';
  8. $my->cache_dir ='cache/';
  9. ?>

index.php

  1. <?php
  2. include 'conf.php';
  3. $my->assign('content','welcome to arrival');
  4. $my->assign('mylife',array("生活","吃饭","梦想","凉风","报仇","fuck someone","fuck you die"));
  5. $my->display('kk.html');
  6. ?>

template下的kk.html【index.php的显示文件】

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>无标题文档</title>
  6. </head>
  7. <body>
  8. {$content}
  9. {section name=truelife loop=$mylife}
  10. {$mylife[truelife]}
  11. {/section}
  12. <p>{html_select_time use_24_hours=true} </p>
  13. <form name="form1" method="post" action="submit.php">
  14. <p>
  15. <label for="conteng"></label>
  16. <input type="text" name="conteng" />
  17. </p>
  18. <p>
  19. <label for="ct"></label>
  20. <textarea name="ct" cols="45" rows="5"></textarea>
  21. </p>
  22. <input type="submit" name="submit" value="提交" />
  23. </form>
  24. <p>&nbsp;</p>
  25. </body>
  26. </html>

submit.php

  1. <?php
  2. include 'conf.php';
  3. $kk=$_POST['conteng'];
  4. $ct=$_POST['ct'];
  5. $sm=new Smarty();
  6. $sm->assign('content',$kk);
  7. $k=explode("\r\n",$ct); //将字符分隔为数组
  8. $sm->assign('ml',$k);
  9. $sm->display('m.tpl');
  10. ?>

template下的m.tpl文件【submit.php的显示文件】

  1. {$content}
  2. {section name=kk loop=$ml}
  3. {$ml[kk]}<br />
  4. {/section}