Bug #22772 Version Retired

Unnecessary usage of using variable by reference in loop

Version: 2.11.3 Reporter: Brian Litzinger

This is an archived bug report. If you are experiencing a similar issue, upgrade to the latest release and if that does not solve the problem, submit a new bug report

Using the following templates:

layouts/main.html resources/index.html which has a {layout="layouts/main"} tag at the top and also has instances of {layout:set name="apples" value="oranges"} site/404.html which has a {layout="layouts/main"} tag at the top

I have a page at .com/resources/foobar, which throws a 404. In the resources/index template I have a {redirect="404"}, which in turn ends up showing the site/404 template to the user. In layouts/main I have several usages of the In:sert extension which basically just uses get_file_contents on a template and puts the template contents into the config->_global_vars array. In the template its used as {in:sert:partials/_footer-top-en} and its using the template_post_parse hook to do its thing.

The issue I have is that when displaying the 404 template only from a {redirect="404"} tag the Template parser is mysteriously taking the value property from the last {layout:set} tag that is on the page containing the {redirect} tag and putting that value into the last key of the config->_global_vars array at some point during the parse process.

For example if the last element of the array is this:

ee()->config->_global_vars[‘in:sert:partials/_footer-top-en’] = ‘… html from a file_get_contents() …’;

Before final output it becomes this:

ee()->config->_global_vars[‘in:sert:partials/_footer-top-en’] = ‘oranges’;

I narrowed down the cause to this loop in the template parser. Changing the loop to not use the $val variable as a reference fixes the issue.

Change this:

foreach (ee()->config->_global_vars as $key => &$val)
{
    // in case any of these variables have EE comments of their own
    // removing from the value makes snippets more usable in conditionals
    $val = $this->remove_ee_comments($val);

    $replace = $this->wrapInContextAnnotations(
        $val,
        'Snippet "'.$key.'"'
    );


    $this->template = str_replace(LD.$key.RD, $replace, $this->template);
}

To this:

foreach (ee()->config->_global_vars as $key => $val)
{
    // in case any of these variables have EE comments of their own
    // removing from the value makes snippets more usable in conditionals
    ee()->config->_global_vars[$key] = $this->remove_ee_comments($val);

    $replace = $this->wrapInContextAnnotations(
        $val,
        'Snippet "'.$key.'"'
    );

    $this->template = str_replace(LD.$key.RD, $replace, $this->template);
}

That loop is simple and there isn’t really any gain in using $val by reference.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases