blob: 0e6ab63d81a31490d0345d7d4b25860033132725 [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * Parser Class
32 *
33 * @package CodeIgniter
34 * @subpackage Libraries
35 * @category Parser
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/libraries/parser.html
38 */
39class CI_Parser {
40
41 var $l_delim = '{';
42 var $r_delim = '}';
43 var $object;
Derek Allardd8270582010-01-15 17:10:56 +000044
Derek Allard2067d1a2008-11-13 22:59:24 +000045 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -050046 * Parse a template
Derek Allard2067d1a2008-11-13 22:59:24 +000047 *
Derek Allardd8270582010-01-15 17:10:56 +000048 * Parses pseudo-variables contained in the specified template view,
Derek Allard2067d1a2008-11-13 22:59:24 +000049 * replacing them with the data in the second param
50 *
51 * @access public
52 * @param string
53 * @param array
54 * @param bool
55 * @return string
56 */
Greg Aker857c6f52010-11-11 17:28:49 -060057 public function parse($template, $data, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +000058 {
59 $CI =& get_instance();
60 $template = $CI->load->view($template, $data, TRUE);
Derek Allardd8270582010-01-15 17:10:56 +000061
62 return $this->_parse($template, $data, $return);
63 }
64
65 // --------------------------------------------------------------------
66
67 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -050068 * Parse a String
Derek Allardd8270582010-01-15 17:10:56 +000069 *
70 * Parses pseudo-variables contained in the specified string,
71 * replacing them with the data in the second param
72 *
73 * @access public
74 * @param string
75 * @param array
76 * @param bool
77 * @return string
78 */
79 function parse_string($template, $data, $return = FALSE)
80 {
81 return $this->_parse($template, $data, $return);
82 }
83
84 // --------------------------------------------------------------------
85
86 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -050087 * Parse a template
Derek Allardd8270582010-01-15 17:10:56 +000088 *
89 * Parses pseudo-variables contained in the specified template,
90 * replacing them with the data in the second param
91 *
92 * @access public
93 * @param string
94 * @param array
95 * @param bool
96 * @return string
97 */
98 function _parse($template, $data, $return = FALSE)
99 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 if ($template == '')
101 {
102 return FALSE;
103 }
Derek Allardd8270582010-01-15 17:10:56 +0000104
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 foreach ($data as $key => $val)
106 {
107 if (is_array($val))
108 {
Barry Mienydd671972010-10-04 16:33:58 +0200109 $template = $this->_parse_pair($key, $val, $template);
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 }
111 else
112 {
113 $template = $this->_parse_single($key, (string)$val, $template);
114 }
115 }
Derek Allardd8270582010-01-15 17:10:56 +0000116
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 if ($return == FALSE)
118 {
Derek Allardd8270582010-01-15 17:10:56 +0000119 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 $CI->output->append_output($template);
121 }
Derek Allardd8270582010-01-15 17:10:56 +0000122
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 return $template;
124 }
Barry Mienydd671972010-10-04 16:33:58 +0200125
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200127
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500129 * Set the left/right variable delimiters
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 *
131 * @access public
132 * @param string
133 * @param string
134 * @return void
135 */
136 function set_delimiters($l = '{', $r = '}')
137 {
138 $this->l_delim = $l;
139 $this->r_delim = $r;
140 }
Barry Mienydd671972010-10-04 16:33:58 +0200141
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200143
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500145 * Parse a single key/value
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 *
147 * @access private
148 * @param string
149 * @param string
150 * @param string
151 * @return string
152 */
153 function _parse_single($key, $val, $string)
154 {
155 return str_replace($this->l_delim.$key.$this->r_delim, $val, $string);
156 }
Barry Mienydd671972010-10-04 16:33:58 +0200157
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200159
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500161 * Parse a tag pair
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500163 * Parses tag pairs: {some_tag} string... {/some_tag}
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 *
165 * @access private
166 * @param string
167 * @param array
168 * @param string
169 * @return string
170 */
171 function _parse_pair($variable, $data, $string)
Barry Mienydd671972010-10-04 16:33:58 +0200172 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 if (FALSE === ($match = $this->_match_pair($string, $variable)))
174 {
175 return $string;
176 }
177
178 $str = '';
179 foreach ($data as $row)
180 {
181 $temp = $match['1'];
182 foreach ($row as $key => $val)
183 {
184 if ( ! is_array($val))
185 {
186 $temp = $this->_parse_single($key, $val, $temp);
187 }
188 else
189 {
190 $temp = $this->_parse_pair($key, $val, $temp);
191 }
192 }
Barry Mienydd671972010-10-04 16:33:58 +0200193
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 $str .= $temp;
195 }
Barry Mienydd671972010-10-04 16:33:58 +0200196
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 return str_replace($match['0'], $str, $string);
198 }
Barry Mienydd671972010-10-04 16:33:58 +0200199
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200201
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500203 * Matches a variable pair
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 *
205 * @access private
206 * @param string
207 * @param string
208 * @return mixed
209 */
210 function _match_pair($string, $variable)
211 {
Derek Jones23e796f2010-05-21 12:45:25 -0500212 if ( ! preg_match("|" . preg_quote($this->l_delim) . $variable . preg_quote($this->r_delim) . "(.+?)". preg_quote($this->l_delim) . '/' . $variable . preg_quote($this->r_delim) . "|s", $string, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 {
214 return FALSE;
215 }
Barry Mienydd671972010-10-04 16:33:58 +0200216
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 return $match;
218 }
219
220}
221// END Parser Class
222
223/* End of file Parser.php */
Greg Aker857c6f52010-11-11 17:28:49 -0600224/* Location: ./system/libraries/Parser.php */