Smarty:如何读取存储在数据库中的{$ var}? [英] Smarty: How to read a {$var} stored in the database?
问题描述
我将codeigniter与smarty一起使用。
I use codeigniter with smarty.
我在数据库中存储了一个名为 $ serverName
的变量。我想将其扩展为实际值 Pedrosite
。但是在加载页面时,它显示的恰好是 {$ serverName}
而不是值。
I have a variable stored in the db called $serverName
. I want to expand it to its actual value "Pedrosite"
. But when the page is loaded, it displays exactly {$serverName}
and not the value.
所以我发现使用smarty的fetch函数在stackoverflow上使用以下解决方案:
So I found this solution on stackoverflow, using smarty's fetch function:
$ data ['content']
包含来自
$data['content'] = $this->CI->smarty->fetch('string:'.$data['content']);
这样,我可以显示聪明的变量,例如: {$ smarty .const.FCPATH}
With that, I can display the smarty vars, like: {$smarty.const.FCPATH}
但是我的自定义 $ vars
都没有,会显示在常规模板( .tpl
)中。
But none of my custom $vars
while they can be shown in a regular template (.tpl
).
所以我发现这种变通方法对我来说似乎很棘手:
So I found this workaround that looks very hacky to me:
$this->CI->smarty->assign('serverName', $this->CI->config->item('server_name'));
我可以将其放入我的 __ construct
功能,那么它将影响整个站点,然后正确加载。但我不确定这是否是正确的方法。
I can put this in one of my __construct
function and then it will affect the whole site, and then it loads properly. But I'm not sure it's the right way to proceed at all.
推荐答案
我不太了解您的问题,但是,如果您有变量 $ serverName
并且内容包含 Pedrosite
,则可以这样显示:
I don't really understand your question, but, if you have your variable $serverName
and it content "Pedrosite"
you can display it like that :
$this->smarty->assign('serverName' , $serverName);
$this->smarty->view('/modules/xxxxxx');
并将其显示在您的.html文件中,例如:
And display it in your .html file for example :
<p>{$serverName}</p>
这篇关于Smarty:如何读取存储在数据库中的{$ var}?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!