smarty icon indicating copy to clipboard operation
smarty copied to clipboard

Parent scope in template blocks does not bubble up when it extends another template

Open maryo opened this issue 2 years ago • 0 comments

I'd like to update Smarty in a big legacy project with code like this. It is just a simplfied example which stopped working in v3.1.28. More precisely the last commit where this was working is 54d54e50003cc328dd2e5a7cb14a43292d0dbf3c. So these are the changes affecting this behavior https://github.com/smarty-php/smarty/compare/54d54e50003cc328dd2e5a7cb14a43292d0dbf3c...2ba71090430e3154ca3e502cd143196655258537

See PR https://github.com/smarty-php/smarty/pull/851

index.tpl:

{$test="index"}
{include "./include.tpl"}
test: {$test}

Displays "index". I'd like to propagate "include_base" (see include_base.tpl) without polluting the global/root scope.

include.tpl:

{extends "./include_base.tpl"}
{*$test=$test scope=parent*} {* this also doesn't help because the variable in `include_base.tpl` is assigned later that this code *}

include_base.tpl:

{$test="include_base" scope=parent}

Trying to affect the execution order using blocks

include.tpl:

{extends "./include_base.tpl"}

{block content append}
    {$test=$test scope=parent} {* this also doesn't help because the parent of this template is not "index.tpl" *}
    {*$smarty.template_object->parent->parent->_assignInScope("test", $test)*} {* this is the only hack i came up with that works *}
{/block}

include_base.tpl:

{block content}
    {$test="include_base" scope=parent}
{/block}

Is it a bug or an intended behavior? Shouldn't there be a way how to propagate variable to the parent "scope by scope"?. Our code is legacy and propagation up the tree might even not be a good idea but it is still a BC break that prevents us to update Smarty without fear.

BTW Thanks for everyone who still tries to maintain this library.

maryo avatar Jan 21 '23 15:01 maryo