Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You need to create a variable whose value is <code>{$testVariable_123}</code><br> Then you can call <code>{eval}</code> on it.</p> <p>The problem is, I couldn't find a way to do this in a good manner.<br> Everything that looks reasonably good doesn't work and the options that work are ugly.</p> <p>Maybe you'd consider some changes in your application design?</p> <p>Here's what I managed to get working:</p> <pre><code># file.php: $smarty-&gt;assign("autoValue", 123); $smarty-&gt;assign("testVariable_123", "foo"); //Option 1 $smarty-&gt;assign("anotherValue", "{\$testVariable_123}"); //Option 2 $smarty-&gt;assign("rb", '}'); // Hack to get the right bracket } withou Smarty parsing it. //Option 3 $smarty-&gt;assign("mask", '{$testVariable_%s}'); // pass the full string_format "mask" directly from PHP # file.tpl 1) Uses the $anotherValue from PHP: Plain: {$anotherValue} Evaled: {eval var=$anotherValue} 2) Build the string on Smarty itself: {assign var="yetAnotherValue" value=$autoValue|string_format:"{\$testVariable_%s$rb"} Plain: {$yetAnotherValue} Evaled: {eval var=$yetAnotherValue} 3) Build the string using the mask from php: {assign var="enoughOfValue" value=$autoValue|string_format:$mask} Plain: {$enoughOfValue} Evaled: {eval var=$enoughOfValue} </code></pre> <p>Mostly, the problem is that Smarty won't ignore a closing bracket } or a $variable even if it's in the middle of a string. Escaping with \ doesn't work neither.</p> <p>If you try:</p> <pre><code>{assign var="yetAnotherValue" value="{\$testVariable_$autoValue}"} </code></pre> <p>it will ignore the "} at the end and consider the Smarty statement as:</p> <pre><code>{assign var="yetAnotherValue" value="{\$testVariable_$autoValue} </code></pre> <p>and it will evaluate the $testVariable even tho it was supposed to be escaped.<br> So we will end up with <code>{\123</code> as the value :(</p> <p>Everything I tried ended up stumbling on that issue. If you find a better way, please, make sure you share it here :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload