Welcome Guest, Not a member yet? Register   Sign In
Parser Static Helper
#1

[eluser]wiredesignz[/eluser]
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Parser Static Helper Class
*
* Adapted from CodeIgniter Parser Class
* @copyright    Copyright (c) 2006, EllisLab, Inc.
* @license    http://ellislab.com/codeigniter/user-guide/license.html
*
* Allows the use of functions in parsed templates ie: {@site_url("home"/{page})}
*
* Place this file in application/helpers as parser_helper.php
* and load as needed.
*
* Usage:
* $parsed_view = parser::parse($view, $data);
*
* Version 0.3 (c) Wiredesignz 2008-02-28
**/
class Parser
{
    function parse($view, $data = array())
    {
        if ($template = $this->load->view($view, $data, TRUE))
        {            
            $template = self::_parse($template, $data);
                        
            while(preg_match('/\{\@(\w*)\("(.*)"\)\}/siU', $template, $match))
            {
                $template = str_replace($match[0], $match[1]($match[2]), $template);
            }
                        
            return $template;
        }
    }
    
    function _parse($template, $data)
    {
        foreach ($data as $key => $val)
        {
            if (is_array($val))
            {                
                $template = self::_parse_pair($key, $val, $template);        
            }
            else
            {
                $template = self::_parse_single($key, (string)$val, $template);
            }
        }
        return $template;
    }
    
    function _parse_single($key, $val, $string)
    {
        return str_replace('{'.$key.'}', $val, $string);
    }

    function _parse_pair($variable, $data, $string)
    {    
        if (FALSE === ($match = self::_match_pair($string, $variable)))
        {
            return $string;
        }

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

    function _match_pair($string, $variable)
    {
        if (!preg_match('/{'.$variable.'}(.*){/'.$variable.'}/s', $string, $match))
        {
            return FALSE;
        }
        
        return $match;
    }
}

Note: PHP4 users please change self:: to parser::
#2

[eluser]wiredesignz[/eluser]
Updated:
Now allows variables passed to functions.
#3

[eluser]Avatar[/eluser]
GREAT JOB!!!!! I'm learning alot from you. really, thanks alot.
#4

[eluser]Avatar[/eluser]
can we add this file to the Modular Extensions - (HMVC) zip file?
#5

[eluser]wiredesignz[/eluser]
I just updated the Parser helper, I think this should be kept seperate from the extensions, sorry. Wink
#6

[eluser]Avatar[/eluser]
ok, no problem. I have some different test libraries as well so I know what you mean. coming soon, after I plus it. is a curl based library.




Theme © iAndrew 2016 - Forum software by © MyBB