PageRenderTime 204ms CodeModel.GetById 101ms app.highlight 5ms RepoModel.GetById 96ms app.codeStats 0ms

/halogy/libraries/Parser.php

https://bitbucket.org/haloweb/halogy-1.0/
PHP | 173 lines | 73 code | 22 blank | 78 comment | 9 complexity | 01a947ba42a153c52c60f8dbabf7a48f MD5 | raw file
  1<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2/**
  3 * CodeIgniter
  4 *
  5 * An open source application development framework for PHP 4.3.2 or newer
  6 *
  7 * @package		CodeIgniter
  8 * @author		ExpressionEngine Dev Team
  9 * @copyright	Copyright (c) 2008 - 2009, EllisLab, Inc.
 10 * @license		http://codeigniter.com/user_guide/license.html
 11 * @link		http://codeigniter.com
 12 * @since		Version 1.0
 13 * @filesource
 14 */
 15
 16// ------------------------------------------------------------------------
 17
 18/**
 19 * Parser Class
 20 *
 21 * @package		CodeIgniter
 22 * @subpackage	Libraries
 23 * @category	Parser
 24 * @author		ExpressionEngine Dev Team
 25 * @link		http://codeigniter.com/user_guide/libraries/parser.html
 26 */
 27class CI_Parser {
 28
 29	var $l_delim = '{';
 30	var $r_delim = '}';
 31	var $object;
 32		
 33	/**
 34	 *  Parse a template
 35	 *
 36	 * Parses pseudo-variables contained in the specified template,
 37	 * replacing them with the data in the second param
 38	 *
 39	 * @access	public
 40	 * @param	string
 41	 * @param	array
 42	 * @param	bool
 43	 * @return	string
 44	 */
 45	function parse($template, $data, $return = FALSE)
 46	{
 47		$CI =& get_instance();
 48		$template = $CI->load->view($template, $data, TRUE);
 49		
 50		if ($template == '')
 51		{
 52			return FALSE;
 53		}
 54		
 55		foreach ($data as $key => $val)
 56		{
 57			if (is_array($val))
 58			{
 59				$template = $this->_parse_pair($key, $val, $template);		
 60			}
 61			else
 62			{
 63				$template = $this->_parse_single($key, (string)$val, $template);
 64			}
 65		}
 66		
 67		if ($return == FALSE)
 68		{
 69			$CI->output->append_output($template);
 70		}
 71		
 72		return $template;
 73	}
 74	
 75	// --------------------------------------------------------------------
 76	
 77	/**
 78	 *  Set the left/right variable delimiters
 79	 *
 80	 * @access	public
 81	 * @param	string
 82	 * @param	string
 83	 * @return	void
 84	 */
 85	function set_delimiters($l = '{', $r = '}')
 86	{
 87		$this->l_delim = $l;
 88		$this->r_delim = $r;
 89	}
 90	
 91	// --------------------------------------------------------------------
 92	
 93	/**
 94	 *  Parse a single key/value
 95	 *
 96	 * @access	private
 97	 * @param	string
 98	 * @param	string
 99	 * @param	string
100	 * @return	string
101	 */
102	function _parse_single($key, $val, $string)
103	{
104		return str_replace($this->l_delim.$key.$this->r_delim, $val, $string);
105	}
106	
107	// --------------------------------------------------------------------
108	
109	/**
110	 *  Parse a tag pair
111	 *
112	 * Parses tag pairs:  {some_tag} string... {/some_tag}
113	 *
114	 * @access	private
115	 * @param	string
116	 * @param	array
117	 * @param	string
118	 * @return	string
119	 */
120	function _parse_pair($variable, $data, $string)
121	{	
122		if (FALSE === ($match = $this->_match_pair($string, $variable)))
123		{
124			return $string;
125		}
126
127		$str = '';
128		foreach ($data as $row)
129		{
130			$temp = $match['1'];
131			foreach ($row as $key => $val)
132			{
133				if ( ! is_array($val))
134				{
135					$temp = $this->_parse_single($key, $val, $temp);
136				}
137				else
138				{
139					$temp = $this->_parse_pair($key, $val, $temp);
140				}
141			}
142			
143			$str .= $temp;
144		}
145		
146		return str_replace($match['0'], $str, $string);
147	}
148	
149	// --------------------------------------------------------------------
150	
151	/**
152	 *  Matches a variable pair
153	 *
154	 * @access	private
155	 * @param	string
156	 * @param	string
157	 * @return	mixed
158	 */
159	function _match_pair($string, $variable)
160	{
161		if ( ! preg_match("|".$this->l_delim . $variable . $this->r_delim."(.+?)".$this->l_delim . '/' . $variable . $this->r_delim."|s", $string, $match))
162		{
163			return FALSE;
164		}
165		
166		return $match;
167	}
168
169}
170// END Parser Class
171
172/* End of file Parser.php */
173/* Location: ./system/libraries/Parser.php */