Welcome Guest, Not a member yet? Register   Sign In
Modular Extensions - Parser Module
#1

[eluser]wiredesignz[/eluser]
I wrote this Parser function a while back and modified it for CI today.

It allows you to embed functions and data in a template.

Could someone or two take a look and see if the code can be improved or optimised.

Many TIA's Smile

Code:
/**
* Parser Module
*
* Version 0.1 (c) Wiredesignz 2008-02-28
**/
class Parser extends Module
{
    function Parser()
    {
        parent::Module();
    }
    
    function parse($view, $data = array())
    {
        if ($template = $this->load->module->view($view, $data, TRUE))
        {
            while (preg_match('/{(.*)}/siU', $template, $matches)) //extract content between { }
            {
                if (preg_match('/\@(.*)/', $matches[1], $funcs)) //extract @function
                {
                    $_result = $funcs[1](); //run function
                }
                else
                    $_result = '';
                
                if (isset($data["$matches[1]"])) //match $data
                {
                    $_result = $data["$matches[1]"];
                }
                
                //place content into template    
                $template = str_replace($matches[0], $_result, $template);
            }
            return $template;
        }
    }
}
#2

[eluser]wiredesignz[/eluser]
This version uses the existing parser code and allows function calls from templates ie: {@site_url}

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Parser Module for Modular Extensions - HMVC
*
* 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}"}
*
* Version 0.4 (c) Wiredesignz 2008-02-28
**/
class Parser extends Module
{
    function Parser()
    {
        parent::Module();
    }
    
    function parse($view, $data = array())
    {
        if ($template = $this->load->view($view, $data, TRUE))
        {            
            $template = $this->_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 = $this->_parse_pair($key, $val, $template);        
            }
            else
            {
                $template = $this->_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 = $this->_match_pair($string, $variable)))
        {
            return $string;
        }

        $str = '';
        foreach ($data as $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;
        }
        
        return str_replace($match['0'], $str, $string);
    }

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

[eluser]wiredesignz[/eluser]
Also available on the wiki: http://codeigniter.com/wiki/Modular_Exte...er_Module/
#4

[eluser]Molchy[/eluser]
Hi,

I am using:
- Latest version CI
- Latest version HMVC
- Codeigniter Parser Class

Problem is that function $this->parser->parse('something', $data); doesn't work inside modules Sad it says can not load requested file. But it works in main controllers "application/controllers".

MY SYSTEM:
- "application/controllers/..." -> Are Modules Loader into base template (Using Parser)
- "Modules beeing loaded" -> Also using parser !!! BREAKING !!! $this->load->view works

I tryed found solution and tryed few tweaks but nothing works Sad I would like to use HMVC and codeigniter Parser class Sad

Any ideas solution ?

Above library also doesnt work with new HMVC Sad it has pass 4 years.




Theme © iAndrew 2016 - Forum software by © MyBB