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

Templates including other templates in loop can't access variables

    XMLWordPrintable

Details

    • Improvement
    • Status: Unverified Fix (View Workflow)
    • Minor
    • Resolution: Fixed
    • 3.0.9
    • 3.1.0-b1
    • Template Engine
    • 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

      Attachments

        Activity

          People

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

            Dates

              Created:
              Updated:
              Resolved: