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 'var swap' a 'var pair'?

Development and Programming

mweichert's avatar
mweichert
68 posts
16 years ago
mweichert's avatar mweichert

Hello,

I have a template which looks like the following:

{iterate param='value'}

{items} … {/items}

{/iterate}

I’m trying to find the {iterate} var pair using the following, but it doesn’t work:

if (preg_match_all("/".LD."iterate param=['\"](.*)['\"]".RD."(.*)".LD.SLASH.'iterate'.RD."/s", $tagdata, $matches)) {
}

Any suggestions? I’m sure it’s just a matter of tweaking the regex.

Thanks for your help.

Mike

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
16 years ago
Mark Bowen's avatar Mark Bowen

Hiya,

If you are creating your own plugin then the way I usually throw out values is in this sort of way :

foreach ($TMPL->var_single as $key => $val)
            {
                $title = $query->row['title'];
                if ($key == "title")
                {
                $tagdata = $TMPL->swap_var_single($key, $title, $tagdata);
                }

            }

            $this->return_data .= $tagdata;
}

That kind of thing. Does that help at all?

Best wishes,

Mark

       
Daniel Walton's avatar
Daniel Walton
553 posts
16 years ago
Daniel Walton's avatar Daniel Walton

There is already a function to grab the data between var pairs, IIRC it is named get_data_between_var_pairs().

Also there is a function that mistakenly you may assume replaces the contents of the var pair with data, but it doesn’t. You will have to use a str_replace or regex replace alternative to achieve it - the function just removes the {var_pair}… {/var_pair} tags.

       
mweichert's avatar
mweichert
68 posts
16 years ago
mweichert's avatar mweichert

Mark, your suggestion is a good way of handling var_single variables, but not var_pair.

Daniel, I tried to use $TMPL->fetch_data_between_var_pairs and $TMPL->swap_var_pairs, but unfortunately neither could match my template. I found the correct regex pattern to use:

if (preg_match_all("/\{iterate param=['\"](.*)['\"]\}(((\s+)?.*(?<!\{\/iterate\}))*)\{\/iterate\}/", $tagdata, $matches, PREG_SET_ORDER)) {
          foreach ($matches as $match) {
            $var_pair = $match[0];
            $param = $match[1];
            $var_pair_tagdata = $match[2];
        }
}

Hope this is some help to anyone else that has a problem swapping var_pair variables.

Thanks for your help guys, Mike

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
16 years ago
Mark Bowen's avatar Mark Bowen

Oops sorry I mis-read what you had written above. Sorry about that.

       
mweichert's avatar
mweichert
68 posts
16 years ago
mweichert's avatar mweichert

Not a problem Mark, I appreciate the reply. It was nice to see an example of what others use for var_single vars too. I typically use $FNS->var_swap.

Cheers, Mike

       
Daniel Walton's avatar
Daniel Walton
553 posts
16 years ago
Daniel Walton's avatar Daniel Walton

What I tend to do is take the data between the var pairs and replace it with a unique marke (much shorter string). I can then do a very easy str_replace to put what I want between the var pairs.

       

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.