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


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

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

我遵循了Smarty.net上关于如何创建自定义检索类型的教程。

我最初的方法是将tpl标记保存到db,然后下载它并将其作为变量传递给smarty分配的变量,并在我的text.pl中执行类似{$source}的操作

,但这将其解析为字符串,smarty不会编译它。

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正在从。我不知道应该把标记加载到。

我觉得这样做很不体面,但是:

这里有一个比你引用的页面更相关的手册页。

特别要注意display方法调用中传递的"filename"的string:前缀。您可以简单地从数据库中提取模板数据(IMNSHO是一个可怕的想法),并以这种方式将其传递给Smarty。没有必要构建你自己的自定义资源的东西,除非你真的想那样做。

就我而言,我会敦促你尽可能让它变得愚蠢、丑陋和明显,以提醒你自己这是一个多么可怕、多么糟糕的想法。我想提醒你Smarty 会编译成PHP,并且将Smarty代码放在数据库中是任意代码执行的有效入口,这是一个严重的安全漏洞。