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

Parse tag pair multiple times

Development and Programming

airways's avatar
airways
154 posts
about 15 years ago
airways's avatar airways

There was a bug report that got me thinking about parsing variable pairs multiple times (https://support.ellislab.com/bugs/detail/15458/). I can see why someone would want the functionality as described in the official reply (calling a tag pair multiple times returns a different row from an array specific to that tag pair), but I have never seen a plugin that behaved that way.

I expected that parse_variables would work the same way it does for single variables - i.e. that no matter how many times I refer to a variable pair, it would perform exactly the same substitution.

At any rate, I wrote this class awhile ago to parse a single set of data for a tag pair multiple times and thought I’d share.

Usage:

$rowdata = $this->EE->bm_parser->parse_variables($tagdata, &$row_vars, array('fieldrows', 'fields'));

// $tagdata is the string to perform replacements on
// $row_vars is the actual array of data (same as the built-in parse_variables)
// $pairs is an array of pair keys from the $row_vars array to perform tag pair substitution on
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * @package ProLib
 * @author Isaac Raway <[email protected]>
 *
 * Copyright (c)2009, 2010. Isaac Raway and MetaSushi, LLC. All rights reserved.
 *
 * This source is commercial software. Use of this software requires a site license for each
 * domain it is used on. Use of this software or any of it's source code without express
 * written permission in the form of a purchased commercial or other license is prohibited.
 *
 * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
 * PARTICULAR PURPOSE.
 *
 **/

class Bm_parser {
    function __construct()
    {
        $this->EE = &get;_instance();
    }
    
    function parse_variables($rowdata, &$row_vars, $pairs)
    {
        // sadly we cannot use parse_variables_row because it only parses each tag_pair once! WTF!
        // we *have* to have multiple tag pairs support so that you can have fun stuff like a row
        // of headers for a table, as well as each row of data, powered by {fields}...{/fields}.

        // prep basic conditionals
        $rowdata = $this->EE->functions->prep_conditionals($rowdata, $row_vars);

        // parse single variables
        foreach ($this->EE->TMPL->var_single as $key => $val)
        {
            if(array_key_exists($key, $row_vars))
            {
                $rowdata = $this->EE->TMPL->swap_var_single($key, $row_vars[$key], $rowdata);
            }
        }

        foreach($pairs as $var_pair)
        {
            foreach ($this->EE->TMPL->var_pair as $key => $val)
            {
                if(strpos($key, $var_pair) === 0)
                {
                    $count = preg_match_all($pattern = "/".LD.$key.RD."(.*?)".LD."\/".$var_pair.RD."/s", $rowdata, $matches);
                    // if we got some matches
                    if($count > 0)
                    {
                        // $matches[0] is an array of the full pattern matches
                        // $matches[1] is an array of the contents of the inside of each variable pair
                        for($i = 0; $i < count($matches[0]); $i++)
                        {
                            $pair_tag = &$matches[0][$i];
                            $pair_row_template = &$matches[1][$i];

                            $pair_data = '';
                            foreach($row_vars[$var_pair] as $data)
                            {
                                $pair_row_data = $pair_row_template;
                                $pair_row_data = $this->EE->functions->prep_conditionals($pair_row_data, $data);
                                foreach($data as $k => $v)
                                {
                                    if(is_array($v)) {
                                        $pair_row_data = $this->parse_variables($pair_row_data, $data, array($k));
                                        /*echo $k;
                                        var_dump($v);
                                        die;*/
                                    } else {
                                        $pair_row_data  = $this->EE->TMPL->swap_var_single($k, $v, $pair_row_data);
                                    }
                                }
                                $pair_data .= $pair_row_data;
                            }
                            ///echo $fields_data;

                            $rowdata = str_replace($pair_tag, $pair_data, $rowdata);
                        }
                    }

                }
            }
        }

        return $rowdata;
    }
}
       

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.