Smarty: How to read a {$var} stored in the database?

蓝咒 提交于 2019-12-11 08:34:47

问题


I use codeigniter with smarty.

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.

So I found this solution on stackoverflow, using smarty's fetch function:

$data['content'] contains the text from the database.

$data['content'] = $this->CI->smarty->fetch('string:'.$data['content']);

With that, I can display the smarty vars, like: {$smarty.const.FCPATH}

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'));

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.


回答1:


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');

And display it in your .html file for example :

<p>{$serverName}</p>


来源:https://stackoverflow.com/questions/55524492/smarty-how-to-read-a-var-stored-in-the-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!