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