简体   繁体   English

Smarty输出动态变量

[英]Smarty output dynamic variable

My goal is to assign a dynamic variable (called skill) to a mathematic equation, like this: 我的目标是为数学方程式分配一个动态变量(称为技能),如下所示:

$skill_HULK = 5, $skill_MAC = 2, ...

So I have done this 所以我做到了

${math assign="skill_`$sk->skill_abbreviation`" equation="x * y" x=1 y=2}

where 哪里

$sk->skill_abbreviation returns HULK and MAC (foreach loop).

But how can I output the result of my variable ? 但是,如何输出变量的结果呢? I tried with "eval", but it only show me the name of my variable. 我尝试使用“ eval”,但是它只显示我变量的名称。 If I do : 如果我做 :

{eval var="sk_`$sk->skill_abbreviation`"}

it will output for example skill_MAC instead of 2. 它将输出例如skill_MAC而不是2。

Can somebody help me please ? 有人可以帮我吗? Thanks 谢谢

Not sure is this what you're looking for, but please take a look: 不确定这是您要找的东西,但是请看一下:

PHP: PHP:

$smarty->assign("skill_abbreviation", "HULK");
$smarty->assign("skill_HULK", 5);
$smarty->assign("skill_MAC", 2);
$smarty->display('index.tpl');

Template: 模板:

{$skill_{$skill_abbreviation}}
// or
{assign var='myVar' value=$skill_{$skill_abbreviation}}
{$myVar}

Result: 结果:

5
// or
5


Or you can run this in loop: 或者,您可以循环运行此命令:

PHP: PHP:

{foreach from=$skill_abbreviations item=abbr}
  {$skill_{$abbr}}
{/foreach}

Template: 模板:

  5
  2

Result: 结果:

  5 2 

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

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