blob: 9f6a814ae4b24e63c66cc039ee71eddc1f177bf3 [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
16// ------------------------------------------------------------------------
17
18/**
19 * Parser Class
20 *
21 * @package CodeIgniter
22 * @subpackage Libraries
23 * @category Parser
24 * @author Rick Ellis
25 * @link http://www.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 $OUT =& _load_class('CI_Output');
48
49 $obj =& get_instance();
50 $template = $obj->load->view($template, '', TRUE);
51
52 if ($template == '')
53 {
54 return FALSE;
55 }
56
57 foreach ($data as $key => $val)
58 {
59 if ( ! is_array($val))
60 {
61 $template = $this->_parse_single($key, $val, $template);
62 }
63 else
64 {
65 $template = $this->_parse_pair($key, $val, $template);
66 }
67 }
68
69 if ($return == FALSE)
70 {
71 $OUT->final_output = $template;
72 }
73
74 return $template;
75 }
76 // END set_method()
77
78 // --------------------------------------------------------------------
79
80 /**
81 * Set the left/right variable delimiters
82 *
83 * @access public
84 * @param string
85 * @param string
86 * @return void
87 */
88 function set_delimiters($l = '{', $r = '}')
89 {
90 $this->l_delim = $l;
91 $this->r_delim = $r;
92 }
93 // END set_method()
94
95 // --------------------------------------------------------------------
96
97 /**
98 * Parse a single key/value
99 *
100 * @access private
101 * @param string
102 * @param string
103 * @param string
104 * @return string
105 */
106 function _parse_single($key, $val, $string)
107 {
108 return str_replace($this->l_delim.$key.$this->r_delim, $val, $string);
109 }
110 // END set_method()
111
112 // --------------------------------------------------------------------
113
114 /**
115 * Parse a tag pair
116 *
117 * Parses tag pairs: {some_tag} string... {/some_tag}
118 *
119 * @access private
120 * @param string
121 * @param array
122 * @param string
123 * @return string
124 */
125 function _parse_pair($variable, $data, $string)
126 {
127 if (FALSE === ($match = $this->_match_pair($string, $variable)))
128 {
129 return $string;
130 }
131
132 $str = '';
133 foreach ($data as $row)
134 {
135 $temp = $match['1'];
136 foreach ($row as $key => $val)
137 {
138 if ( ! is_array($val))
139 {
140 $temp = $this->_parse_single($key, $val, $temp);
141 }
142 else
143 {
144 $temp = $this->_parse_pair($key, $val, $temp);
145 }
146 }
147
148 $str .= $temp;
149 }
150
151 return str_replace($match['0'], $str, $string);
152 }
153 // END set_method()
154
155 // --------------------------------------------------------------------
156
157 /**
158 * Matches a variable pair
159 *
160 * @access private
161 * @param string
162 * @param string
163 * @return mixed
164 */
165 function _match_pair($string, $variable)
166 {
167 if ( ! preg_match("|".$this->l_delim . $variable . $this->r_delim."(.+)".$this->l_delim . '/' . $variable . $this->r_delim."|s", $string, $match))
168 {
169 return FALSE;
170 }
171
172 return $match;
173 }
174 // END _match_pair()
175
176}
177// END Parser Class
178?>