简体   繁体   中英

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. If I do :

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

it will output for example skill_MAC instead of 2.

Can somebody help me please ? Thanks

Not sure is this what you're looking for, but please take a look:

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:

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

Template:

  5
  2

Result:

  5 2 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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