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

How to parse inner tags of a tag pair

Development and Programming

bohara's avatar
bohara
42 posts
16 years ago
bohara's avatar bohara

Hi everyone. I am working on my first module and I do not understand how to loop over the inner tags of template tag pairs. The code below is just a simplified version for clarity.

Here is my template:

{exp:module:items}

    {overview}

    {details}
        {variation}
        {size}
        {weight}
    {/details}
    
{/exp:module:items}

Here is the PHP:

function items()
{
    global $DB, $TMPL, $IN, $FNS;
    
    $tagdata = $TMPL->tagdata;
    
    $itemlist = $DB->query("SELECT * FROM exp_module_items");
    
            
    foreach ($TMPL->var_pair as $key => $val)
    {
        if (ereg("^details", $key))
        {
            foreach($itemlist->result as $row)
            {
                
                //$temp = preg_match("/".LD."$key".RD."(.*?)".LD.SLASH.'details'.RD."/s", $tagdata, $matches);
        
                $tagdata = str_replace('{variation}', $row['variation'], $tagdata);
                
            }
            
        }
    
    }

    $tagdata = str_replace('{overview}', 'Something', $tagdata);
    
     return $tagdata;
    
}

The $temp variable just returns the number of matches for ‘details’. How do you parse and loop the inner variables. Right now I only get one row returned for the {variation}. Any ideas. The documentation on this is not very clear to me.

Thanks.

       
Robert Wallis's avatar
Robert Wallis
36 posts
16 years ago
Robert Wallis's avatar Robert Wallis

To iterate details do something like this:

function items()
{
    global $DB, $TMPL, $IN, $FNS;
    
    $tagdata = $TMPL->tagdata;
    
    $itemlist = $DB->query("SELECT * FROM exp_module_items");
                
    foreach ($TMPL->var_pair as $key => $val)
    {
        if (ereg("^details", $key))
        {
             // render to this string
            $details = '';
            
            // get just stuff inside {details}{/details}
            if (!preg_match("/".LD."$key".RD."(.*?)".LD.SLASH."$key".RD."/s", $tagdata, $matches))
                continue;

            $details_tagdata = $matches[1];
               
            foreach($itemlist->result as $row)
            {
                // make a copy of the unprocessed text
                $temp = $details_tagdata;
                
                // do normal modifications
                $temp = str_replace('{variation}', $row['variation'], $temp);
                
                // render the results of this iteration
                $details .= $temp;
            }
            
            // replace the {details}{/details} tag with all the results
            $tagdata = preg_replace("/".LD."$key".RD."(.*?)".LD.SLASH."$key".RD."/s", $details, $tagdata);
        }
    }
    $tagdata = str_replace('{overview}', 'Something', $tagdata);
    return $tagdata;   
}

😊

       
bohara's avatar
bohara
42 posts
16 years ago
bohara's avatar bohara

Thanks for the reply! I had moved on to other things, and never spent the time to figure this out.

       
bohara's avatar
bohara
42 posts
16 years ago
bohara's avatar bohara

Works perfectly. Thanks again

       

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.