Search code examples
phpsmarty

smarty foreach attribute name not working


The issue here is that the 'name' attribute in the 'foreach' loop doesn't allow dynamic values, so using a variable or expression in the name attribute like name=foo_{$_id} is not allowed.

<html>
  <head>
    <title>Smarty</title>
  </head>
  <body>
    {foreach from=$pages key=m item=i name=foo_{$_id}}

    {/foreach}
  </body>
</html>

As a result, I encounter an error:

[13-Aug-2016 17:30:01 ...] PHP Fatal error:  Uncaught  --> Smarty Compiler: Syntax error in template ""  on line 36 "{foreach from=$test.params.rows key="rowId" item="rowText" name="rows_{$id}"}" 'name' attribute/variable has illegal value

Solution

  • Try:

    {foreach from=$pages key=m item=i name="foo_`$_id`"}