Welcome Guest, Not a member yet? Register   Sign In
Using the Parser library with flat arrays?
#1

[eluser]Matthew Pennell[/eluser]
I like to stick to the Parser library in my views, but one problem I have is that it doesn't seem to allow you to pass in one-dimensional arrays.

So this:

Code:
$data['blog_entries'] = array(
    array('title' => 'Title 1', 'body' => 'Body 1'),
    array('title' => 'Title 2', 'body' => 'Body 2'),
    array('title' => 'Title 3', 'body' => 'Body 3'),
    array('title' => 'Title 4', 'body' => 'Body 4'),
    array('title' => 'Title 5', 'body' => 'Body 5')
);

becomes this:

Code:
{blog_entries}
<h5>{title}</h5>
<p>{body}</p>
{/blog_entries}

But what if I want to pass in this:

Code:
$data['countries'] = array('England', 'Ireland', 'Scotland', 'Wales');

?
#2

[eluser]Pascal Kriete[/eluser]
It *should* become the equivalent of
Code:
$countries = array('England', 'Ireland'...);
in your view.
#3

[eluser]Matthew Pennell[/eluser]
Never mind, I made my own; here it is, in case anyone else ever needs the same thing:

Code:
&lt;?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* MY_Parser Class
*
* @package        CodeIgniter
* @subpackage    Libraries
* @category    MY_Parser
* @author        Matthew Pennell
*/
class MY_Parser extends CI_Parser {

    /**
     *  Parse a tag pair
     *
     * Parses tag pairs:  {some_tag} string... {/some_tag}
     *
     * Also parses flat arrays, using {__value__} as a placeholder
     *
     * @access    private
     * @param    string
     * @param    array
     * @param    string
     * @return    string
     */
    function _parse_pair($variable, $data, $string)
    {    
        if (FALSE === ($match = $this->_match_pair($string, $variable)))
        {
            return $string;
        }

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

So now this:

Code:
$data['countries'] = array('England', 'Ireland', 'Scotland', 'Wales');

can be passed to the Parser library and output using this kind of thing:

Code:
<ul>
{countries}
  <li>{__value__}</li>
{/countries}
</ul>

Smile
#4

[eluser]Phil Sturgeon[/eluser]
Technically those arrays you posted at the top are both one-dimensional arrays, the 2nd just doesnt have custom keys.

Thats a good contribution, the parser library definatly needs some more work done.

Also dont forget you can chuck in plain PHP code to the parser views. If something doesnt work in parser syntax and it NEEDS to be done soon... chuck in some PHP. ;-)
#5

[eluser]Matthew Pennell[/eluser]
[quote author="thepyromaniac" date="1202194649"]Also dont forget you can chuck in plain PHP code to the parser views. If something doesnt work in parser syntax and it NEEDS to be done soon... chuck in some PHP. ;-)[/quote]
Where's the fun in that? Wink




Theme © iAndrew 2016 - Forum software by © MyBB