Search code examples
phpsmartyblock

Can smarty block names be derived from smarty variables


I would like to define smarty block names according to smarty data, but I can't seem to do it.
Example:

{foreach $array as $code}
  {block name=block_$code}
     <div id='{$code}'></div>
  {/block}
{/foreach}

My purpose is to extend a specific block_$code block by a child template. Is this possible or is there some other trick I could use to do this?

Thanks.


Solution

  • I was able to find the following link from 2011 indicating that this wasn't possible at that time. I suspect it still isn't:

    http://www.smarty.net/forums/viewtopic.php?t=19805&highlight=block%20variable%20name

    The good news it that I was able to figure out how to make my code work without it. I wanted to be able to override just one of the divs defined by the foreach. Here's how I can do it:

    Parent:

    {foreach $array as $code}
        {block name=code_loop}
            <div>Normal Stuff</div>
        {/block}
    {/foreach}
    

    Child:

    {block name=code_loop}
        {if $code == 'code of interest'}
            <div>New Stuff</div>
        {else}
            {$smarty.block.parent}
        {/if}
    {/block}