• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CI Parser Mod

#1
[eluser]CodyPChristian[/eluser]
Been awhile since I've been around the forums, actually its been almost 2 years!! Either way I still develop with CI daily. While developing the other day I came across a little problem while using one of the CI libs. I come across little things like this alot but I never post them Wink

Let me explain first the use of this mod, then I will share the code for it.

In my pacific case I needed a way to pass a template from a table to the parser class along-side the array of parsing variables for the parser to replace with. I needed it to allow that action as well as to return to a variable. The latter you can do by default via the 3rd param, however it expects that you pass in a view file and not a variable containing the data. This way I could then pass the data onto another function. Maybe there is another way to do this but this worked in my case.

Example Usage:
Code:
$TemplateParams = //Array of template parsing variables to replace the {vars} within $EmailTemplate
$EmailTemplate = //This is the email template data coming from database as an array...
//Parse Templates        
$this->load->library('parser');
//Parse Email Subject
$subject = $this->parser->parse($EmailTemplate['email_subject'], $TemplateParams, TRUE, TRUE); //4th Param 'true' added to library to bypass view output
//Parse Email Body            
$body = $this->parser->parse($EmailTemplate['email_body'], $TemplateParams, TRUE, TRUE); //4th Param 'true' added to library to bypass view output

Parser.php Mod
/system/libraries/parser.php
Code:
#/system/libraries/parser.php

/**
*  Parse a template
*
* Parses pseudo-variables contained in the specified template,
* replacing them with the data in the second param
*
* @access    public
* @param    string
* @param    array
* @param    bool
* @return    string
*/

/**
* Modified 'parse' Function
*
* Added 'bypass' param and if statements to function
* to allow bypass of $CI->load->view() output
*
* @author  Cody P. Christian
* @date    8:25PM EST 12/7/09
* @url        http://codypchristian.net
*
*/
function parse($template, $data, $return = FALSE, $bypass = FALSE)
{        
    //if bypass equals false then continue, else ignore
    if(!$bypass){    
        $CI =& get_instance();
        $template = $CI->load->view($template, $data, TRUE);
    }
    
    if ($template == '')
    {
        return FALSE;
    }

    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);
        }
    }
    //if bypass equals false then continue, else ignore
    if(!$bypass){
        if ($return == FALSE)
        {
            $CI->output->append_output($template);
        }
    }
    
    return $template;
}
What I modified:
I modified the function by adding the 4th param $bypass and adding the two !$bypass if statements.

To use the previous function just replace your parser() function in your parser.php file.

I do not know if adding my 'mod comment' is allowed by Ellis, however I add it to all files I modify to keep track of when I modified them.

I hope the code above is self-explanatory but if you have any questions let me know. In basic form it allows me to pass data in from a $variable instead of a view and return to a $variable.

#2
[eluser]Phil Sturgeon[/eluser]
Good to see you post again dude, it's been a while!

Why not just extend the Parser class instead of modifying the core?

application/libraries/MY_Parser.php

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed.');

class MY_Parser extends CI_Parser {
/**
* Modified 'parse' Function
*
* Added 'bypass' param and if statements to function
* to allow bypass of $CI->load->view() output
*
* @author  Cody P. Christian
* @date    8:25PM EST 12/7/09
* @url        http://codypchristian.net
*
*/
function parse($template, $data, $return = FALSE, $bypass = FALSE)
{        
    //if bypass equals false then continue, else ignore
    if(!$bypass){    
        $CI =& get_instance();
        $template = $CI->load->view($template, $data, TRUE);
    }
    
    if ($template == '')
    {
        return FALSE;
    }

    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);
        }
    }
    //if bypass equals false then continue, else ignore
    if(!$bypass){
        if ($return == FALSE)
        {
            $CI->output->append_output($template);
        }
    }
    
    return $template;
}

}

#3
[eluser]CodyPChristian[/eluser]
Phil, you have a point, I could have done it that way too, just wasn't thinking I guess Wink Thanks.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.