Smarty使用自定义资源的方法

这篇文章主要介绍了Smarty使用自定义资源的方法,实例分析了smarty自定义资源的定义与使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下,本文实例讲述了Smarty使用自定义资源的方法,分享给大家供大家参考,具体如下:

  1. <?php
  2. // put these function somewhere in your application
  3. function db_get_template ($tpl_name, &$tpl_source, &$smarty_obj)
  4. {
  5. // do database call here to fetch your template,
  6. // populating $tpl_source
  7. $sql = new SQL;
  8. $sql->query("select tpl_source
  9. from my_table
  10. where tpl_name='$tpl_name'");
  11. if ($sql->num_rows) {
  12. $tpl_source = $sql->record['tpl_source'];
  13. return true;
  14. } else {
  15. return false;
  16. }
  17. }
  18. function db_get_timestamp($tpl_name, &$tpl_timestamp, &$smarty_obj)
  19. {
  20. // do database call here to populate $tpl_timestamp.
  21. $sql = new SQL;
  22. $sql->query("select tpl_timestamp
  23. from my_table
  24. where tpl_name='$tpl_name'");
  25. if ($sql->num_rows) {
  26. $tpl_timestamp = $sql->record['tpl_timestamp'];
  27. return true;
  28. } else {
  29. return false;
  30. }
  31. }
  32. function db_get_secure($tpl_name, &$smarty_obj)
  33. {
  34. // assume all templates are secure
  35. return true;
  36. }
  37. function db_get_trusted($tpl_name, &$smarty_obj)
  38. {
  39. // not used for templates
  40. }
  41. // register the resource name "db"
  42. $smarty->register_resource("db", array("db_get_template",
  43. "db_get_timestamp",
  44. "db_get_secure",
  45. "db_get_trusted"));
  46. // using resource from php script
  47. $smarty->display("db:index.tpl");
  48. ?>

希望本文所述对大家基于smarty的php程序设计有所帮助。