blob: a0b60ed976166aaa087b451f3889a513134c8394 [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 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @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
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Parser Class
30 *
31 * @package CodeIgniter
32 * @subpackage Libraries
33 * @category Parser
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000035 * @link http://codeigniter.com/user_guide/libraries/parser.html
36 */
37class CI_Parser {
38
Timothy Warren86611db2012-04-27 10:06:25 -040039 /**
40 * Left delimeter character for psuedo vars
41 *
42 * @var string
43 */
Andrey Andreevee3bc292011-12-24 01:44:13 +020044 public $l_delim = '{';
Andrey Andreev56454792012-05-17 14:32:19 +030045
Timothy Warren86611db2012-04-27 10:06:25 -040046 /**
47 * Right delimeter character for psuedo vars
48 *
49 * @var string
50 */
Andrey Andreevee3bc292011-12-24 01:44:13 +020051 public $r_delim = '}';
Andrey Andreev56454792012-05-17 14:32:19 +030052
Timothy Warren86611db2012-04-27 10:06:25 -040053 /**
54 * Reference to CodeIgniter instance
55 *
56 * @var object
57 */
Andrey Andreev74476e12012-03-26 15:03:40 +030058 protected $CI;
Derek Allardd8270582010-01-15 17:10:56 +000059
Derek Allard2067d1a2008-11-13 22:59:24 +000060 /**
Andrey Andreev74476e12012-03-26 15:03:40 +030061 * Parse a template
Derek Allard2067d1a2008-11-13 22:59:24 +000062 *
Derek Allardd8270582010-01-15 17:10:56 +000063 * Parses pseudo-variables contained in the specified template view,
Derek Allard2067d1a2008-11-13 22:59:24 +000064 * replacing them with the data in the second param
65 *
Derek Allard2067d1a2008-11-13 22:59:24 +000066 * @param string
67 * @param array
68 * @param bool
69 * @return string
70 */
Greg Aker857c6f52010-11-11 17:28:49 -060071 public function parse($template, $data, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +000072 {
Andrey Andreevee3bc292011-12-24 01:44:13 +020073 $this->CI =& get_instance();
74 $template = $this->CI->load->view($template, $data, TRUE);
Derek Allardd8270582010-01-15 17:10:56 +000075
76 return $this->_parse($template, $data, $return);
77 }
78
79 // --------------------------------------------------------------------
80
81 /**
Andrey Andreev74476e12012-03-26 15:03:40 +030082 * Parse a String
Derek Allardd8270582010-01-15 17:10:56 +000083 *
84 * Parses pseudo-variables contained in the specified string,
85 * replacing them with the data in the second param
86 *
Derek Allardd8270582010-01-15 17:10:56 +000087 * @param string
88 * @param array
89 * @param bool
90 * @return string
91 */
Andrey Andreevee3bc292011-12-24 01:44:13 +020092 public function parse_string($template, $data, $return = FALSE)
Derek Allardd8270582010-01-15 17:10:56 +000093 {
94 return $this->_parse($template, $data, $return);
95 }
96
97 // --------------------------------------------------------------------
98
99 /**
Andrey Andreev74476e12012-03-26 15:03:40 +0300100 * Parse a template
Derek Allardd8270582010-01-15 17:10:56 +0000101 *
102 * Parses pseudo-variables contained in the specified template,
103 * replacing them with the data in the second param
104 *
Derek Allardd8270582010-01-15 17:10:56 +0000105 * @param string
106 * @param array
107 * @param bool
108 * @return string
109 */
Andrey Andreev74476e12012-03-26 15:03:40 +0300110 protected function _parse($template, $data, $return = FALSE)
Derek Allardd8270582010-01-15 17:10:56 +0000111 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 if ($template == '')
113 {
114 return FALSE;
115 }
Derek Allardd8270582010-01-15 17:10:56 +0000116
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 foreach ($data as $key => $val)
118 {
Andrey Andreev56454792012-05-17 14:32:19 +0300119 $template = is_array($val)
120 ? $this->_parse_pair($key, $val, $template)
121 : $template = $this->_parse_single($key, (string) $val, $template);
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 }
Derek Allardd8270582010-01-15 17:10:56 +0000123
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 if ($return == FALSE)
125 {
Andrey Andreevee3bc292011-12-24 01:44:13 +0200126 $this->CI->output->append_output($template);
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 }
Derek Allardd8270582010-01-15 17:10:56 +0000128
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 return $template;
130 }
Barry Mienydd671972010-10-04 16:33:58 +0200131
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200133
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 /**
Andrey Andreev74476e12012-03-26 15:03:40 +0300135 * Set the left/right variable delimiters
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 * @param string
138 * @param string
139 * @return void
140 */
Andrey Andreevee3bc292011-12-24 01:44:13 +0200141 public function set_delimiters($l = '{', $r = '}')
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 {
143 $this->l_delim = $l;
144 $this->r_delim = $r;
145 }
Barry Mienydd671972010-10-04 16:33:58 +0200146
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200148
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 /**
Andrey Andreev74476e12012-03-26 15:03:40 +0300150 * Parse a single key/value
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 * @param string
153 * @param string
154 * @param string
155 * @return string
156 */
Andrey Andreev74476e12012-03-26 15:03:40 +0300157 protected function _parse_single($key, $val, $string)
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 {
Andrey Andreevee3bc292011-12-24 01:44:13 +0200159 return str_replace($this->l_delim.$key.$this->r_delim, (string) $val, $string);
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 }
Barry Mienydd671972010-10-04 16:33:58 +0200161
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200163
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 /**
Andrey Andreev74476e12012-03-26 15:03:40 +0300165 * Parse a tag pair
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 *
Andrey Andreev74476e12012-03-26 15:03:40 +0300167 * Parses tag pairs: {some_tag} string... {/some_tag}
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 * @param string
170 * @param array
171 * @param string
172 * @return string
173 */
Andrey Andreev74476e12012-03-26 15:03:40 +0300174 protected function _parse_pair($variable, $data, $string)
Barry Mienydd671972010-10-04 16:33:58 +0200175 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 if (FALSE === ($match = $this->_match_pair($string, $variable)))
177 {
178 return $string;
179 }
180
181 $str = '';
182 foreach ($data as $row)
183 {
Andrey Andreevee3bc292011-12-24 01:44:13 +0200184 $temp = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 foreach ($row as $key => $val)
186 {
Andrey Andreev56454792012-05-17 14:32:19 +0300187 $temp = is_array($val)
188 ? $this->_parse_pair($key, $val, $temp)
189 : $this->_parse_single($key, $val, $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 }
Barry Mienydd671972010-10-04 16:33:58 +0200191
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 $str .= $temp;
193 }
Barry Mienydd671972010-10-04 16:33:58 +0200194
Andrey Andreevee3bc292011-12-24 01:44:13 +0200195 return str_replace($match[0], $str, $string);
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 }
Barry Mienydd671972010-10-04 16:33:58 +0200197
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200199
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 /**
Andrey Andreev74476e12012-03-26 15:03:40 +0300201 * Matches a variable pair
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 * @param string
204 * @param string
205 * @return mixed
206 */
Andrey Andreev74476e12012-03-26 15:03:40 +0300207 protected function _match_pair($string, $variable)
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 {
Andrey Andreev74476e12012-03-26 15:03:40 +0300209 return 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',
210 $string, $match)
211 ? $match : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 }
213
214}
Derek Allard2067d1a2008-11-13 22:59:24 +0000215
216/* End of file Parser.php */
Andrey Andreev74476e12012-03-26 15:03:40 +0300217/* Location: ./system/libraries/Parser.php */