smarty如何获取存放在数据库中的模板

subendong 2010-04-19 08:27:56
比如一段代码:
<html><head><title>{{$title}}</title></head><body>{{$content}}</body></html>
我可以将其放入一个file.tpl的文件里面,对不对?
然后我写PHP代码的时候,只要$smarty->display("file.tpl");就可以了。
我现在不放file.tpl里面了,我要放在数据库里,请问smarty有没有什么函数能让我传递从数据库中读取的模板数据?然后又再做处理。
谢谢!!
...全文
181 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
subendong 2010-04-20
  • 打赏
  • 举报
回复
在线等待。。。
mrshelly 2010-04-20
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 subendong 的回复:]

我知道这段代码,但我悟性低,不知道怎么用,能举例吗?
谢谢!!!
[/Quote]

链接中的代码就是很好的例子....
悟性低就努力提高自己的悟性...
subendong 2010-04-20
  • 打赏
  • 举报
回复
<?php
// put these function somewhere in your application
function db_get_template ($tpl_name, &$tpl_source, &$smarty_obj)
{
// do database call here to fetch your template,
// populating $tpl_source
$sql = new SQL;
$sql->query("select tpl_source
from my_table
where tpl_name='$tpl_name'");
if ($sql->num_rows) {
$tpl_source = $sql->record['tpl_source'];
return true;
} else {
return false;
}
}

// register the resource name "db"
$smarty->register_resource("db", array("db_get_template"));

// using resource from php script
$smarty->display("db:index.tpl");
?>

疑问:
1、函数db_get_template()里面的形参&$tpl_source前面的&符号是什么意思?三个形参各代表什么意思?什么作用?
2、看看我说的意思对不?
我数据库里有个模板表,有一个模板的名字叫index.tpl,我直接这么用就可以了是吗?$smarty->display("db:index.tpl");
当然前面db_get_template()函数还有$smarty->register_resource("db", array("db_get_template"));要加上去。
subendong 2010-04-20
  • 打赏
  • 举报
回复
我知道这段代码,但我悟性低,不知道怎么用,能举例吗?
谢谢!!!
cooledit2730 2010-04-20
  • 打赏
  • 举报
回复

<?php
// put these function somewhere in your application
function db_get_template ($tpl_name, &$tpl_source, &$smarty_obj)
{
// do database call here to fetch your template,
// populating $tpl_source
$sql = new SQL;
$sql->query("select tpl_source
from my_table
where tpl_name='$tpl_name'");
if ($sql->num_rows) {
$tpl_source = $sql->record['tpl_source'];
return true;
} else {
return false;
}
}

function db_get_timestamp($tpl_name, &$tpl_timestamp, &$smarty_obj)
{
// do database call here to populate $tpl_timestamp.
$sql = new SQL;
$sql->query("select tpl_timestamp
from my_table
where tpl_name='$tpl_name'");
if ($sql->num_rows) {
$tpl_timestamp = $sql->record['tpl_timestamp'];
return true;
} else {
return false;
}
}

function db_get_secure($tpl_name, &$smarty_obj)
{
// assume all templates are secure
return true;
}

function db_get_trusted($tpl_name, &$smarty_obj)
{
// not used for templates
}

// register the resource name "db"
$smarty->register_resource("db", array("db_get_template",
"db_get_timestamp",
"db_get_secure",
"db_get_trusted"));

// using resource from php script
$smarty->display("db:index.tpl");
?>
recher1114 2010-04-20
  • 打赏
  • 举报
回复
没看明白
jlzan1314 2010-04-19
  • 打赏
  • 举报
回复
今天我终于恍然大悟了
mrshelly 2010-04-19
  • 打赏
  • 举报
回复
http://www.smarty.net/manual/en/template.resources.php

Templates from other sources


就是你的需求...

没事就多看手册.

21,887

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧