Search code examples
phparrayscsvtemplate-talphptal

PHPTAL and CSV in array


I have data in CSV format:

$result->names = array(array('x,y,z'),array('a,b,c'));

for example:

item.names = 'a,b,c'

this code doesn't work:

<div tal:repeat="item result">
<div tal:repeat="x php:explode(',',${item.names})" tal:omit-tag="">
<span tal:content="x"></span>
</div></div>

but this code works, why?

<div tal:repeat="item result">
<div tal:repeat="x php:explode(',','a,b,c')" tal:omit-tag="">
<span tal:content="x"></span>
</div></div>

Solution

  • Don't use ${} in expressions with php: modifier. That should work, assuming you're looping over value from $result->names:

    <tal:block tal:repeat="x php:explode(',', item)">
    

    (BTW: instead of omit-tag you can use tal:block element which is "invisible")