繁体   English   中英

如何使用smarty模板引擎保存模板并从数据库下载模板

[英]How can I save a template and download it from the database using smarty template engine

我很难将模板保存到数据库并下载内容以使用Smarty查看。

我遵循Smarty.net上的教程,了解如何制作自定义检索类型。

我原来的方法是将tpl标记保存到数据库,然后下载它,并将其作为变量传递给一个聪明的分配变量,然后在我的text.pl中执行类似{$ source}的操作

但这会将其解析为字符串,并且聪明的不会编译它。

smarty网站上的教程没有帮助,只是让我有更多问题...

这是我根据教程尝试过的

function db_get_template($tpl_name, &$tpl_source, &$s)
{
    $tpl_name = "index.tpl";
    $tpl_source = nl2br($defaults->get_default_theme('theme_data'));
    return true;
}
function db_get_timestamp($tpl_name, &$tpl_timestamp, &$smarty)
{
    $tpl_timestamp = (int)time();
    //$tpl_timestamp = (int)$defaults->get_default_theme('modified_on');
    return true;
}

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

function db_get_trusted($tpl_name, &$smarty)
{
    // not used for templates 
}
$s->registerResource("db", array( 
    "db_get_template",
    "db_get_secure",
    "db_get_trusted"
));
$s->display("db:index.tpl");

我不确定db:index.tpl是从哪里提取的。 我不知道标记应该在哪里加载...

我什至建议这样做,但感觉很脏,但是:

这是您参考页面 更相关的手册页面

特别要注意string:display方法调用中传递的“文件名”的前缀。 您可以简单地从数据库中提取模板数据(一个可怕的想法,IMNSHO),然后将其传递给Smarty。 除非您确实想这样做,否则无需构建自己的自定义资源。

我要敦促您使它尽可能愚蠢,丑陋和明显,以提醒自己这是一个多么可怕的,不好的主意。 我想提醒您,Smarty可以编译为PHP ,并且将Smarty代码放入数据库中可以有效地执行任意代码,这是一个严重的安全漏洞。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM