Is it possible to put variables inside config files?

流过昼夜 提交于 2019-12-23 22:20:06

问题


I use smarty to allow different languages on my site, which works OK so far. I store the texts in config files in different sections.

But then there are sentences like this:

"You have 6 new mails!", which would be in german "Sie haben 6 neue Mails!"

Now there's text before the number and behind the number, which is loaded from the database. And I would like to put it into the config file and just load the number on its own.

so I have this in my "text.conf"

[en]
mail_count = "You have $NUMBER new mails!"
[de]
mail_count = "Sie haben $NUMBER neue Mails!"

and this in my "show_text.php"

$smarty->assign('NUMBER', 6);

Is something like this possible? Maybe with Smarty 3.0?

Thanks in advance, BH


回答1:


You can use the sprintf sintax. This example comes from a pager-like thing:

results = "Results %s to %s of %s total"

{#results#|sprintf:$start:$end:$total}



回答2:


I just tried this and it works, but it's rather ugly...

  • create a file "number.tpl" which contains

{$NUMBER}

  • in your conf file go like this

mail_count = "You have {include file='number.tpl'} new mails!"

I guess it's because the smarty variables only work in tpl files.




回答3:


When reading the config file, you need to open it using

$cfg = $smarty->fetch('path/to/file');

After that you have the whole files content ind the $cfg variable, with {$NUMBER} replaced.




回答4:


I don't have Smarty to test this right now but it should work if you properly declare the variable in your config entry, like:

ail_count = "You have {$NUMBER} new mails!"


来源:https://stackoverflow.com/questions/3863310/is-it-possible-to-put-variables-inside-config-files

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