简体   繁体   English

Smarty将var分配给配置文件vars

[英]Smarty assign var to config file vars

My question looks almost the same as this Is it possible to put variables inside config files? 我的问题看起来几乎与此相同是否可以将变量放入配置文件中?

I have a config file that controls the language. 我有一个控制语言的配置文件。 Also I have a feedback div that is showed for the feedback. 另外,我还有一个显示反馈的div。

Now I want to assign feedback like this: 现在,我要分配这样的反馈:

$smarty->assign('feedback', 'the_age_of_user_' . $user->name . '_is_changed_to_' . $user->age . '_years');

nl.conf nl.conf

the_age_of_user_%s_is_changed_to_%s_years = De leeftijd van gebruiker %s gewijzigd naar %s jaar the_age_of_user_%s_is_changed_to_%s_years = De leeftijd van gebruiker %s gewijzigd naar %s jaar

en.conf en.conf

the_age_of_user_%s_is_changed_to_%s_years = The age of user %s is changed to %s years the_age_of_user_%s_is_changed_to_%s_years = The age of user %s is changed to %s years

Does anyone know how I can accomplish this? 有人知道我该怎么做吗? Or is there a better solution to assign variables to the config file? 还是有更好的解决方案来将变量分配给配置文件?

Sorry, but I don't completely understand what are you trying to accomplish. 抱歉,但我不完全了解您要完成的工作。

In our project, we have following solution for similar problem: 在我们的项目中,针对类似问题,我们有以下解决方案:

Language file: 语言文件:

the_age_of_user_is_changed = The age of user {user} is changed to {year} years

Notice that there is no %s or any variables on left side. 请注意,左侧没有%s或任何变量。 It is always constant. 它总是恒定的。

Option 1: 选项1:

  1. Smarty/code assigment: $smarty->assign("VARIABLE_TO_USE_IN_TEMPLATE", getLang('the_age_of_user_is_changed', array('user' => $user->name,'year' => $user->age)); Smarty /代码$smarty->assign("VARIABLE_TO_USE_IN_TEMPLATE", getLang('the_age_of_user_is_changed', array('user' => $user->name,'year' => $user->age));$smarty->assign("VARIABLE_TO_USE_IN_TEMPLATE", getLang('the_age_of_user_is_changed', array('user' => $user->name,'year' => $user->age));

  2. Then, in Smarty, you can use following code: {$VARIABLE_TO_USE_IN_TEMPLATE} 然后,在Smarty中,您可以使用以下代码: {$VARIABLE_TO_USE_IN_TEMPLATE}

Function getLang reads string information from language files and replaces all {symbol} to corresponding data. 函数getLang从语言文件中读取字符串信息,并将所有{symbol}替换为相应的数据。

Option 2: 选项2:

  1. In template, we have smarty userspace function: {LANG key='the_age_of_user_is_changed' name=$user->name year=$user->age} Lang is actually calls function getLang from previous example. 在模板中,我们有一个聪明的用户空间函数: {LANG key='the_age_of_user_is_changed' name=$user->name year=$user->age} Lang实际上是前面示例中的函数getLang。

  2. In code you have to assign user: $smarty->assign("user", $user); 在代码中,您必须分配用户: $smarty->assign("user", $user); . After that, smarty will do everything by itself. 在那之后,聪明的人会自己做所有的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM