When extending a template, is it possible to change a blocks parent without affecting its nested children? I've tried {$smarty.block.parent}
, but it displays the outer parents content, not the block itself. Smarty Version 3.1.12
{block name="block_parent"}
<div class="parent">
<h1>Parent Title</h1>
{block name="block_child"}
<div class="child">Child</div>
{/block}
</div>
{/block}
{extends file="parent.tpl"}
{block name="block_parent"}
<div class="changed--parent">
<h1>Parent Title changed</h1>
{block name="block_child"}
{* something like this for example *}
{$smarty.block.self}
{/block}
</div>
{/block}
<div class="changed--parent">
<h1>Parent Title changed</h1>
<div class="child">Child</div>
</div>
As reply to @sofl:
Output when using {$smarty.block.parent}
<div class="changed--parent">
<h1>Parent Title changed</h1>
<div class="parent">
<h1>Parent Title</h1> <!-- this is not desired to happen -->
<div class="child">Child</div>
</div>
</div>
Output when using {$smarty.block.child}
<div class="changed--parent">
<h1>Parent Title changed</h1>
<!-- child div missing -->
</div>
Unfortunately there is no solution, or this is not possible in smarty at this moment. But there is a workaround, which - at least - works in my case:
{block name="parentBlockName"}
{capture name="parentBlockName"}
{$smarty.block.parent}
{/capture}
{$smarty.capture.parentBlockName|replace:'thing_to_change':'how_it_should_be'}
{/block}
This will replace contents in your parent, while preserving children. Try to be as specific as possible to make sure nothing wrong gets replaced. I've used this workaround to change classes within the parent element.