blob: 5bc2eb5a3082b97fb5737d668c5cf43f0a3bf7e5 [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * Code Igniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author Rick Ellis
9 * @copyright Copyright (c) 2006, pMachine, Inc.
10 * @license http://www.codeignitor.com/user_guide/license.html
11 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
admine79dc712006-09-26 03:52:45 +000016// INITIALIZE THE CLASS ---------------------------------------------------
17
18$obj =& get_instance();
19$obj->parser =& new CI_Parser();
20
adminb0dd10f2006-08-25 17:25:49 +000021// ------------------------------------------------------------------------
22
23/**
24 * Parser Class
25 *
26 * @package CodeIgniter
27 * @subpackage Libraries
28 * @category Parser
29 * @author Rick Ellis
30 * @link http://www.codeigniter.com/user_guide/libraries/parser.html
31 */
32class CI_Parser {
33
34 var $l_delim = '{';
35 var $r_delim = '}';
36 var $object;
37
38 /**
39 * Parse a template
40 *
41 * Parses pseudo-variables contained in the specified template,
42 * replacing them with the data in the second param
43 *
44 * @access public
45 * @param string
46 * @param array
47 * @param bool
48 * @return string
49 */
50 function parse($template, $data, $return = FALSE)
51 {
adminb0dd10f2006-08-25 17:25:49 +000052 $obj =& get_instance();
admin05633d72006-09-21 17:22:21 +000053 $template = $obj->load->view($template, $data, TRUE);
adminb0dd10f2006-08-25 17:25:49 +000054
55 if ($template == '')
56 {
57 return FALSE;
58 }
59
60 foreach ($data as $key => $val)
61 {
admin4e9f3f92006-09-23 17:39:20 +000062 if (is_string($val))
adminb0dd10f2006-08-25 17:25:49 +000063 {
64 $template = $this->_parse_single($key, $val, $template);
65 }
admin4e9f3f92006-09-23 17:39:20 +000066 elseif (is_array($val))
adminb0dd10f2006-08-25 17:25:49 +000067 {
68 $template = $this->_parse_pair($key, $val, $template);
69 }
70 }
71
72 if ($return == FALSE)
73 {
admin212a3fa2006-09-23 04:22:39 +000074 $obj->output->final_output = $template;
adminb0dd10f2006-08-25 17:25:49 +000075 }
76
77 return $template;
78 }
79 // END set_method()
80
81 // --------------------------------------------------------------------
82
83 /**
84 * Set the left/right variable delimiters
85 *
86 * @access public
87 * @param string
88 * @param string
89 * @return void
90 */
91 function set_delimiters($l = '{', $r = '}')
92 {
93 $this->l_delim = $l;
94 $this->r_delim = $r;
95 }
96 // END set_method()
97
98 // --------------------------------------------------------------------
99
100 /**
101 * Parse a single key/value
102 *
103 * @access private
104 * @param string
105 * @param string
106 * @param string
107 * @return string
108 */
109 function _parse_single($key, $val, $string)
110 {
111 return str_replace($this->l_delim.$key.$this->r_delim, $val, $string);
112 }
113 // END set_method()
114
115 // --------------------------------------------------------------------
116
117 /**
118 * Parse a tag pair
119 *
120 * Parses tag pairs: {some_tag} string... {/some_tag}
121 *
122 * @access private
123 * @param string
124 * @param array
125 * @param string
126 * @return string
127 */
128 function _parse_pair($variable, $data, $string)
129 {
130 if (FALSE === ($match = $this->_match_pair($string, $variable)))
131 {
132 return $string;
133 }
134
135 $str = '';
136 foreach ($data as $row)
137 {
138 $temp = $match['1'];
139 foreach ($row as $key => $val)
140 {
141 if ( ! is_array($val))
142 {
143 $temp = $this->_parse_single($key, $val, $temp);
144 }
145 else
146 {
147 $temp = $this->_parse_pair($key, $val, $temp);
148 }
149 }
150
151 $str .= $temp;
152 }
153
154 return str_replace($match['0'], $str, $string);
155 }
156 // END set_method()
157
158 // --------------------------------------------------------------------
159
160 /**
161 * Matches a variable pair
162 *
163 * @access private
164 * @param string
165 * @param string
166 * @return mixed
167 */
168 function _match_pair($string, $variable)
169 {
170 if ( ! preg_match("|".$this->l_delim . $variable . $this->r_delim."(.+)".$this->l_delim . '/' . $variable . $this->r_delim."|s", $string, $match))
171 {
172 return FALSE;
173 }
174
175 return $match;
176 }
177 // END _match_pair()
178
179}
180// END Parser Class
181?>