Discuz教程网

Smarty使用自定义资源

[复制链接]
authicon 09927306 发表于 2011-1-12 16:58:16 | 显示全部楼层 |阅读模式

  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.   
  19. function db_get_timestamp($tpl_name, &$tpl_timestamp, &$smarty_obj)   
  20. {   
  21.     // do database call here to populate $tpl_timestamp.   
  22.     $sql = new SQL;   
  23.     $sql->query("select tpl_timestamp   
  24.                    from my_table   
  25.                   where tpl_name=\'$tpl_name\'");   
  26.     if ($sql->num_rows) {   
  27.         $tpl_timestamp = $sql->record[\'tpl_timestamp\'];   
  28.         return true;   
  29.     } else {   
  30.         return false;   
  31.     }   
  32. }   
  33.   
  34. function db_get_secure($tpl_name, &$smarty_obj)   
  35. {   
  36.     // assume all templates are secure   
  37.     return true;   
  38. }   
  39.   
  40. function db_get_trusted($tpl_name, &$smarty_obj)   
  41. {   
  42.     // not used for templates   
  43. }   
  44.   
  45. // register the resource name "db"   
  46. $smarty->register_resource("db", array("db_get_template",   
  47.                                        "db_get_timestamp",   
  48.                                        "db_get_secure",   
  49.                                        "db_get_trusted"));   
  50.   
  51. // using resource from php script   
  52. $smarty->display("db:index.tpl");   
  53. ?>
复制代码






上一篇:php soap
下一篇:简单的图片处理类
authicon TRACYFLYING 发表于 2011-6-17 06:01:09 | 显示全部楼层
支持!好东西,拿走了!
authicon 21585151 发表于 2011-6-20 03:01:04 | 显示全部楼层
前来看看那
authicon 陶衣小可 发表于 2011-6-22 01:59:53 | 显示全部楼层
不错,我喜欢
authicon lilac_yao 发表于 2011-6-24 00:59:55 | 显示全部楼层
好东西,要下来看看
authicon lanyuqing 发表于 2011-6-25 06:59:40 | 显示全部楼层
这个要顶起来啊
authicon melody0721 发表于 2011-6-25 08:00:04 | 显示全部楼层
顶的就是你
authicon 蓝色天空k 发表于 2011-6-27 06:00:11 | 显示全部楼层
哈哈  收了你
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-2 22:00

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表