Is it possible to put variables inside config files?
I use smarty to allow different languages on my site, which works OK开发者_JAVA百科 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
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}
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.
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.
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!"
精彩评论