blob: 39aa61e4536e9c1ddb6b7ea6f6d3adf90ae2a76f [file] [log] [blame]
Andrey Andreevee3bc292011-12-24 01:44:13 +02001<?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
Andrey Andreevee3bc292011-12-24 01:44:13 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevee3bc292011-12-24 01:44:13 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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
Andrey Andreevee3bc292011-12-24 01:44:13 +020041 public $l_delim = '{';
42 public $r_delim = '}';
43 public $object;
44 private $CI;
Derek Allardd8270582010-01-15 17:10:56 +000045
Derek Allard2067d1a2008-11-13 22:59:24 +000046 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -050047 * Parse a template
Derek Allard2067d1a2008-11-13 22:59:24 +000048 *
Derek Allardd8270582010-01-15 17:10:56 +000049 * Parses pseudo-variables contained in the specified template view,
Derek Allard2067d1a2008-11-13 22:59:24 +000050 * replacing them with the data in the second param
51 *
52 * @access public
53 * @param string
54 * @param array
55 * @param bool
56 * @return string
57 */
Greg Aker857c6f52010-11-11 17:28:49 -060058 public function parse($template, $data, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +000059 {
Andrey Andreevee3bc292011-12-24 01:44:13 +020060 $this->CI =& get_instance();
61 $template = $this->CI->load->view($template, $data, TRUE);
Derek Allardd8270582010-01-15 17:10:56 +000062
63 return $this->_parse($template, $data, $return);
64 }
65
66 // --------------------------------------------------------------------
67
68 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -050069 * Parse a String
Derek Allardd8270582010-01-15 17:10:56 +000070 *
71 * Parses pseudo-variables contained in the specified string,
72 * replacing them with the data in the second param
73 *
74 * @access public
75 * @param string
76 * @param array
77 * @param bool
78 * @return string
79 */
Andrey Andreevee3bc292011-12-24 01:44:13 +020080 public function parse_string($template, $data, $return = FALSE)
Derek Allardd8270582010-01-15 17:10:56 +000081 {
82 return $this->_parse($template, $data, $return);
83 }
84
85 // --------------------------------------------------------------------
86
87 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -050088 * Parse a template
Derek Allardd8270582010-01-15 17:10:56 +000089 *
90 * Parses pseudo-variables contained in the specified template,
91 * replacing them with the data in the second param
92 *
Andrey Andreevee3bc292011-12-24 01:44:13 +020093 * @access private
Derek Allardd8270582010-01-15 17:10:56 +000094 * @param string
95 * @param array
96 * @param bool
97 * @return string
98 */
Andrey Andreevee3bc292011-12-24 01:44:13 +020099 private function _parse($template, $data, $return = FALSE)
Derek Allardd8270582010-01-15 17:10:56 +0000100 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 if ($template == '')
102 {
103 return FALSE;
104 }
Derek Allardd8270582010-01-15 17:10:56 +0000105
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 foreach ($data as $key => $val)
107 {
108 if (is_array($val))
109 {
Barry Mienydd671972010-10-04 16:33:58 +0200110 $template = $this->_parse_pair($key, $val, $template);
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 }
112 else
113 {
114 $template = $this->_parse_single($key, (string)$val, $template);
115 }
116 }
Derek Allardd8270582010-01-15 17:10:56 +0000117
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 if ($return == FALSE)
119 {
Andrey Andreevee3bc292011-12-24 01:44:13 +0200120 $this->CI->output->append_output($template);
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 }
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 */
Andrey Andreevee3bc292011-12-24 01:44:13 +0200136 public function set_delimiters($l = '{', $r = '}')
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 {
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 */
Andrey Andreevee3bc292011-12-24 01:44:13 +0200153 private function _parse_single($key, $val, $string)
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 {
Andrey Andreevee3bc292011-12-24 01:44:13 +0200155 return str_replace($this->l_delim.$key.$this->r_delim, (string) $val, $string);
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 }
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 */
Andrey Andreevee3bc292011-12-24 01:44:13 +0200171 private 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 {
Andrey Andreevee3bc292011-12-24 01:44:13 +0200181 $temp = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 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
Andrey Andreevee3bc292011-12-24 01:44:13 +0200197 return str_replace($match[0], $str, $string);
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 }
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 */
Andrey Andreevee3bc292011-12-24 01:44:13 +0200210 private function _match_pair($string, $variable)
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 {
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 */