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

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

Code:
<?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);
    }
    
}
?>

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


Messages In This Thread
Using the Parser library with flat arrays? - by El Forum - 02-04-2008, 10:42 AM
Using the Parser library with flat arrays? - by El Forum - 02-04-2008, 10:57 AM
Using the Parser library with flat arrays? - by El Forum - 02-04-2008, 10:58 AM
Using the Parser library with flat arrays? - by El Forum - 02-04-2008, 06:57 PM
Using the Parser library with flat arrays? - by El Forum - 02-05-2008, 01:05 AM



Theme © iAndrew 2016 - Forum software by © MyBB