Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 7.5k
Modular Extensions Parser Module
Derek Jones edited this page Jul 5, 2012
·
14 revisions
Category:Module | Category:Module::Parser
This parser module is designed for use with the Modular_Extensions_-_HMVC system. It allows the use of function calls from inside parsed templates.
<?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://codeigniter.com/user_guide/license.html* * Allows the use of functions in parsed templates ie: {@site_url("home/{page}"}** Version 0.7 (c) Wiredesignz 2008-06-13**/class Parser extends Controller
{
functionParser()
{
parent::Controller();
}
functionparse($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 ($dataas$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)
{
returnstr_replace('{'.$key.'}', $val, $string);
}
function_parse_pair($variable, $data, $string)
{ if (FALSE === ($match = $this->_match_pair($string, $variable)))
{
return$string;
}
$str = '';
foreach ($dataas$row)
{
$temp = $match['1'];
foreach ($rowas$key => $val)
{
if ( ! is_array($val))
{
$temp = $this->_parse_single($key, $val, $temp);
}
else
{
$temp = $this->_parse_pair($key, $val, $temp);
}
}
$str .= $temp;
}
returnstr_replace($match['0'], $str, $string);
}
function_match_pair($string, $variable)
{
if (!preg_match('/{'.$variable.'}(.*){\/'.$variable.'}/s', $string, $match))
{
returnFALSE;
}
return$match;
}
}