Welcome Guest, Not a member yet? Register   Sign In
Little modification for core parser library
#1

[eluser]nutella[/eluser]
Usually I don't need powerful template (I've used smarty with pleasure but mainly to substitute vars), then core parser library could be enough for me. I found only a little annoying problem with row_array result assigned and passed to parser.

I try to explain with example:

Code:
Controller code:

$foo['code']= 'xxx';
$foo['name']= 'test';
$foo['price']= '12.34';

$data= array(
  'product'=>$foo,
  'page'=>'view',
  'menu'=>true
);
$this->parser->parse('product', $data);

In this example I haven't found any way to use product['code'], product['name'] and product['price'] in the template, because product is an array. However I can't write something like:
Code:
{product.code}
as in smarty, mostly because CI parser start from array and not from the template to make replacement, so the parser doesn't know that product.code will be correct.
At the same time something like:
Code:
{product}{code}{/product}
doesn't work because {tag}{/tag} expects a cycle through multi-dimensional array.

I've done a little modification to Parser.php, modifying _parse_pair to this code:
Code:
function _parse_pair($variable, $data, $string)
    {
        if (FALSE === ($match = $this->_match_pair($string, $variable)))
        {
            return $string;
        }

        $str = '';
        foreach ($data as $row)
        {
            if (is_array($row)) {
                $temp = $match['1'];
                foreach ($row as $key => $val)
                {
                    if ( ! is_array($val))
                    {
                        $temp = $this->_parse_single($key, $val, $temp);
                    }
                    else
                    {
                        $temp = $this->_parse_pair($key, $val, $temp);
                    }
                }
                $str .= $temp;
            }
        }
        foreach ($data as $key => $val)
        {
            $str = $this->_parse_single($key, (string)$val, $str);
        }

        return str_replace($match['0'], $str, $string);
    }
So, now is possibile to write something like: {product}{code}{/product} and the parser works correctly.

I've just tried and tested a little bit and seems to work well.

Let me know if this make sense and will be useful or if somethings is wrong...

Thanks.
#2

[eluser]nutella[/eluser]
Ok, I've found a problem if I use more tags in a single page:
Code:
Code: {product}{code}{/product}
Price: {product}{price}{/product}
doesn't work... only first tag will be replaced... need more work...
#3

[eluser]nutella[/eluser]
Ok, maybe I've correct the code, I've modified also _match_pair function:

Code:
function _parse_pair($variable, $data, $string)
    {
        if (FALSE === ($matches = $this->_match_pair($string, $variable)))
        {
            return $string;
        }

        foreach ($matches as $match)                        /// added
        {
            $str = '';
            foreach ($data as $row)
            {
                if (is_array($row))                            /// added
                {
                    $temp = $match['1'];
                    foreach ($row as $key => $val)
                    {
                        if ( ! is_array($val))
                        {
                            $temp = $this->_parse_single($key, $val, $temp);
                        }
                        else
                        {
                            $temp = $this->_parse_pair($key, $val, $temp);
                        }
                    }
                    $str .= $temp;
                }
            }
            foreach ($data as $key => $val)                    /// added
            {
                $str = $this->_parse_single($key, (string)$val, $str);
            }
            $string= str_replace($match['0'], $str, $string);
        }
        return $string;
    }

    function _match_pair($string, $variable)
    {
        if ( ! preg_match_all("|" . preg_quote($this->l_delim) . $variable . preg_quote($this->r_delim) . "(.+?)". preg_quote($this->l_delim) . '/' . $variable . preg_quote($this->r_delim) . "|s", $string, $match, PREG_SET_ORDER))
        {
            return FALSE;
        }
        return $match;
    }

}

Let me know...




Theme © iAndrew 2016 - Forum software by © MyBB