Question

I am using Smarty 3 template inheritance. I have the following issue:

This is the parent template code:

{foreach from=$items_collection item=single_item name=foo}
  {block name="bar_{$smarty.foreach.foo.index}"}
    some html goes here that I need to make editable if required
  {/block}
{/foreach}

And after this when I extend the template by:

{extends file='path/to/template/file.tpl'}

{block name="bar_0"}random stuff{/block}
{block name="bar_1"}random stuff{block}
...

Or any other dynamically generated block name, it does not change template values... all I get on the child is the parent but without the changed blocks content.

Is there a special syntax or any other technique to achieve dynamic block names.

Was it helpful?

Solution

I don't think there can be. Blocks are not held as variables and then populated in the template when it's displayed, instead they are compiled once and are then static for the cached template.

Template inheritance is a compile time process which creates a single compiled template file. Compared to corresponding solutions based on subtemplates included with the {include} tag it does have much better performance when rendering.

It's hard to give advice about how to do what you want to do, as you're quite vague, but you can probably do what you want with a block function

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top