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

2019-08-01 05:02发布

问题:

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>