Uploaded image for project: 'phpBB3'
  1. phpBB3
  2. PHPBB3-10288

Templates including other templates in loop can't access variables

    Details

    • Type: Improvement
    • Status: Unverified Fix
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 3.0.9
    • Fix Version/s: 3.1.0-b1
    • Component/s: Template Engine
    • Labels:
      None

      Description

      Let's say we have the template viewtopic_body.html with the following code:

      <!-- BEGIN postrow -->
        <!-- INCLUDE viewtopic_post.html -->
      <!-- END postrow -->

      and the template viewtopic_post.html contains this (example reduced to the minimum):

      {postrow.POST_ID}

      the output will be empty. Why? Because the template system creates the following PHP code for the loop:

      <?php } $_postrow_count = (isset($this->_tpldata['postrow'])) ? sizeof($this->_tpldata['postrow']) : 0;if ($_postrow_count) {for ($_postrow_i = 0; $_postrow_i < $_postrow_count; ++$_postrow_i){$_postrow_val = &$this->_tpldata['postrow'][$_postrow_i]; ?>

      To be precise: the "loop data" is assigned to a local variable, called $_postrow_val. However, the inclusion of the other template is done with a class method. It's rendered PHP code would be:

      <?php if (!defined('IN_PHPBB')) exit; ?><?php echo $_postrow_val['POST_ID']; ?>

      ...but $_postrow_val does not exist here anymore. It also can't be accessed using the global keyword (or would require using it at even more places due to variable scopes in PHP).

      A solution to this would be to not use local variables anymore, but class properties. The assignment of the loop code would then look like this:

      $this-> _postrow_val = &$this->_tpldata['postrow'][$_postrow_i];

      and so in the included template a code like this will also succeed in accessing existing data:

      $this-> _postrow_val['POST_ID'];

      I tested this successfully and it would also solve a lot of problems using template includes. Likewise, templates could be unified (displaying private messages and displaying posts could use the same user details template code because they could use a template inclusion).

      For PHP's variable scope across objects there are two comments which demonstrate the problem quite well:
      http://www.php.net/manual/en/language.variables.scope.php#100884
      http://www.php.net/manual/en/language.variables.scope.php#55595

        Activity

        Hide
        AmigoJack AmigoJack added a comment -

        Is there a major reason why the rendered PHP template code uses local variables instead of sticking them to it's very own instance? Because at first sight I see no disadvantage to only use properties instead of variables. To avoid name conflicts all variables to be created could be stored in an array property (I omitted the leading underscore, as it now has no purpose anymore):

        $this-> _tplvar= array();
        $this-> _tplvar['postrow_i']= 0;
        $this-> _tplvar['postrow_val']= &$his-> _tpldata['postrow'][$this-> _tplvar['postrow_i']];

        Show
        AmigoJack AmigoJack added a comment - Is there a major reason why the rendered PHP template code uses local variables instead of sticking them to it's very own instance? Because at first sight I see no disadvantage to only use properties instead of variables. To avoid name conflicts all variables to be created could be stored in an array property (I omitted the leading underscore, as it now has no purpose anymore): $this-> _tplvar= array(); $this-> _tplvar['postrow_i']= 0; $this-> _tplvar['postrow_val']= &$his-> _tpldata['postrow'][$this-> _tplvar['postrow_i']];
        Hide
        Oleg Oleg [X] (Inactive) added a comment -

        As a reminder, the template engine in 3.1 shares very little code with 3.0.

        Show
        Oleg Oleg [X] (Inactive) added a comment - As a reminder, the template engine in 3.1 shares very little code with 3.0.
        Hide
        nickvergessen Joas Schilling added a comment -

        EXreaction [X] is this fixed since twig?

        Show
        nickvergessen Joas Schilling added a comment - EXreaction [X] is this fixed since twig?
        Hide
        EXreaction EXreaction [X] (Inactive) added a comment -

        Yes, this has been fixed. PR is a test for this

        Show
        EXreaction EXreaction [X] (Inactive) added a comment - Yes, this has been fixed. PR is a test for this

          People

          • Assignee:
            EXreaction EXreaction [X] (Inactive)
            Reporter:
            AmigoJack AmigoJack
          • Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development