Search code examples
inheritancesmartytemplate-engine

How to remove content appended to block in parent template?


I have three templates:

base.tpl

{block name="myBlock"}
    base
{/block}

child.tpl

{extends file="base.tpl"}
{block name="myBlock" append}
    child
{/block}

grandchild.tpl

{extends file="child.tpl"}
{block name="myBlock" append}
    grandchild
{/block}

The output when rendering grandchild.tpl is

base
child
grandchild

But I would like to skip the content added by child.tpl, so I would like to get this output:

base
grandchild

Problem is that this needs to be done without changing base.tpl and child.tpl (both come from a third party), I can't just extend base.tpl in grandchild.tpl, since there are more blocks with content in child.tpl I'd like to have in the result.


Solution

  • In this case you should simple change name of the block in base.tpl and grandchild.tpl and leave child.tpl not changed.

    base.tpl

    {block name="myBlock2"}
        base
    {/block}
    

    child.tpl

    {extends file="base.tpl"}
    {block name="myBlock" append}
        child
    {/block}
    

    grandchild.tpl

    {extends file="child.tpl"}
    {block name="myBlock2" append}
        grandchild
    {/block}
    

    Output is now:

    base
    grandchild

    EDIT - after info from comment that base.tpl also could't be modified

    It seems that the only thing you need to do is removing append from grandchild.tpl and leave 2 other files unchanged.

    grandchild.tpl

    {extends file="child.tpl"}
    {block name="myBlock"}
        grandchild
    {/block}
    

    Output is:

    base
    grandchild

    as you wanted