Skip to content

Instantly share code, notes, and snippets.

@hardfire
Created August 30, 2012 06:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hardfire/3523532 to your computer and use it in GitHub Desktop.
Save hardfire/3523532 to your computer and use it in GitHub Desktop.
Parser for Bonfire
<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
* MY Parser
*
* @package Bonfire
* @subpackage Libraries
* @category Parser
* @author Avinash Kundaliya
* @link http://cibonfire.com
*/
class MY_Parser extends CI_Parser
{
/**
* An instance of the CI super object.
*
* @access private
*
* @var object
*/
private $ci;
function __construct()
{
//include the lex parser class
if( ! class_exists('Lex_Parser'))
{
include APPPATH.'/libraries/Lex/Parser.php';
}
$this->ci =& get_instance();
}
public function parse($template, $data, $return = FALSE, $load_view = TRUE)
{
// Convert from object to array
is_array($data) or $data = (array) $data;
$data = array_merge($data, $this->ci->load->_ci_cached_vars);
//if load_view is false, we parse the string
$parseString = $template;
//else load the view to parse
if($load_view)
{
$parseString = $this->ci->load->view($template, $data, TRUE);
}
$parser = new Lex_Parser();
$parser->scopeGlue(':');
$parsed = $parser->parse($parseString, $data, array($this, 'parser_callback'));
if ( ! $return)
{
$this->ci->output->append_output($parsed);
return;
}
return $parsed;
}
public function parser_callback($module, $attribute, $content)
{
$return_view = NULL;
$parsed_return = '';
$output = self::get_view($module,$attribute);
$return_view = $output;
//loop it up, if its array no use in the template, gotta work it here.
if(is_array($output))
{
$parser = new Lex_Parser();
$parser->scopeGlue(':');
foreach($output as $result)
{
$parsed_return .= $parser->parse($content, $result, array($this, 'parser_callback'));
}
unset($parser);
$return_view = $parsed_return;
}
return $return_view;
}
private function get_view($module,$attribute)
{
$return_view = false;
// Get the required module
$module = str_replace(':','/',$module);
$method = 'index';
if(($pos = strrpos($module, '/')) != FALSE) {
$method = substr($module, $pos + 1);
$module = substr($module, 0, $pos);
}
if($class = $this->ci->load->module($module))
{
//if the method is callable
if (method_exists($class, $method))
{
ob_start();
$output = call_user_func_array(array($class, $method), $attribute);
$buffer = ob_get_clean();
$output = ($output !== NULL) ? $output : $buffer;
$return_view = $output;
}
}
//maybe it is a library
else if(!$return_view && strpos($module,'/') === FALSE)
{
if(class_exists($module))
{
ob_start();
$output = call_user_func_array(array($module, $method), $attribute);
$buffer = ob_get_clean();
$output = ($output !== NULL) ? $output : $buffer;
$return_view = $output;
}
}
return $return_view;
}
}
@og-shawn-crigger
Copy link

I have a nicely working copy of this, I used the Autoloader as I had issues with the original gist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment