smarty-php / smarty-php/smarty

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

Open
#850 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
2.3k
Forks
709
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the behavior shown in index.tpl, include.tpl, and include_base.tpl, then review the changes linked in the issue and PR 851. Reproduce the scope behavior against the stated working commit and v3.1.28, and use the result to determine whether parent-scope propagation should be restored without polluting the root scope.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
web-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.