We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Possible bug in EE->TMPL->parse_variables?

Development and Programming

Ralph's avatar
Ralph
78 posts
13 years ago
Ralph's avatar Ralph

Hi. I’m writing a plugin but I’ve noticed a possible bug in EE->TMPL->parse_variables. When I pass an array that contains a tag pair two levels down and the template has two of those tag pairs and the first tag pair doesn’t contain all the tags of the second tag then the second tag pair will not display the tag that is missing from the first tag pair. To get a sense of what I mean here’s the output the code below, notice that the second line shows “{text} {text}” where “blue green” should be.

#00f blue #0f0 green - blue green 
#00f #0f0 - {text} {text}

Here is the function:

public function testEE() {
  $tag = array(
    'colour' =>
    array(
      array('data'=>array(
        array('hex'=>'#00f','text'=>'blue'),
        array('hex'=>'#0f0','text'=>'green')
      )
      )
    ),
  );
  $tag = array($tag);
  $result = $this->EE->TMPL->parse_variables($this->EE->TMPL->tagdata, $tag);
  return $result;
 }

and here is the template code:

{exp:test:testEE}
{colour}{data}{hex} {text} {/data}{/colour} - {colour}{data}{text} {/data}{/colour}
{/exp:test:testEE}
<br>
{exp:test:testEE}
{colour}{data}{hex} {/data}{/colour} - {colour}{data}{text} {/data}{/colour}
{/exp:test:testEE}
       
Ralph's avatar
Ralph
78 posts
13 years ago
Ralph's avatar Ralph

Here’s a nice bodge to fix that example, but it will still have problems if there’s a tag pair further down the branch.

public function testEE() {
  $tag = array(
    'colour' =>
    array(
      array('data'=>array(
        array('hex'=>'#00f','text'=>'blue'),
        array('hex'=>'#0f0','text'=>'green')
      )
      )
    ),
  );
  $tag = array($tag);
  
  return $this->parse_variables_fix($this->EE->TMPL->tagdata, $tag);
 }
 
 private function parse_variables_fix($tagdata,$tag) {
  foreach ($tag[0] as $tag_name => &$tag_value) {
   preg_match_all('/\{' . preg_quote($tag_name) . '\}[\s\S]+?\{\/' . preg_quote($tag_name) . '\}/',$tagdata,$matches);
   for ($i=0;$i<count($matches[0]);$i+=1) {
    $parsed = $this->EE->TMPL->parse_variables($matches[0][$i],$tag);
    $tagdata = str_replace($matches[0][$i],$parsed,$tagdata);
   }
  }
  return $tagdata;
 }
       
Ralph's avatar
Ralph
78 posts
13 years ago
Ralph's avatar Ralph

The bug is still present in the latest version (v2.5.2). How do add-on developers get around this bug?

       
Ralph's avatar
Ralph
78 posts
13 years ago
Ralph's avatar Ralph

Submitted bug report: https://support.ellislab.com/bugs/detail/18182

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.