blob: 760d5d4d3e8432214effe61f773238158ea41ce9 [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.
admine334c472006-10-21 19:44:22 +000010 * @license http://www.codeignitor.com/user_guide/license.html
adminb0dd10f2006-08-25 17:25:49 +000011 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
admine79dc712006-09-26 03:52:45 +000015
adminb0dd10f2006-08-25 17:25:49 +000016// ------------------------------------------------------------------------
17
18/**
19 * Parser Class
admine334c472006-10-21 19:44:22 +000020 *
adminb0dd10f2006-08-25 17:25:49 +000021 * @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 *
admine334c472006-10-21 19:44:22 +000036 * Parses pseudo-variables contained in the specified template,
adminb0dd10f2006-08-25 17:25:49 +000037 * 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 {
admin88a8ad12006-10-07 03:16:32 +000047 $CI =& get_instance();
48 $template = $CI->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 {
admin88a8ad12006-10-07 03:16:32 +000069 $CI->output->final_output = $template;
adminb0dd10f2006-08-25 17:25:49 +000070 }
71
72 return $template;
73 }
adminb0dd10f2006-08-25 17:25:49 +000074
75 // --------------------------------------------------------------------
76
77 /**
78 * Set the left/right variable delimiters
79 *
80 * @access public
81 * @param string
82 * @param string
83 * @return void
84 */
85 function set_delimiters($l = '{', $r = '}')
86 {
87 $this->l_delim = $l;
88 $this->r_delim = $r;
89 }
adminb0dd10f2006-08-25 17:25:49 +000090
91 // --------------------------------------------------------------------
92
93 /**
94 * Parse a single key/value
95 *
96 * @access private
97 * @param string
98 * @param string
99 * @param string
100 * @return string
101 */
102 function _parse_single($key, $val, $string)
103 {
104 return str_replace($this->l_delim.$key.$this->r_delim, $val, $string);
105 }
adminb0dd10f2006-08-25 17:25:49 +0000106
107 // --------------------------------------------------------------------
108
109 /**
110 * Parse a tag pair
111 *
112 * Parses tag pairs: {some_tag} string... {/some_tag}
113 *
114 * @access private
115 * @param string
116 * @param array
117 * @param string
118 * @return string
119 */
120 function _parse_pair($variable, $data, $string)
121 {
122 if (FALSE === ($match = $this->_match_pair($string, $variable)))
123 {
124 return $string;
125 }
126
127 $str = '';
128 foreach ($data as $row)
129 {
130 $temp = $match['1'];
131 foreach ($row as $key => $val)
132 {
133 if ( ! is_array($val))
134 {
135 $temp = $this->_parse_single($key, $val, $temp);
136 }
137 else
138 {
139 $temp = $this->_parse_pair($key, $val, $temp);
140 }
141 }
142
143 $str .= $temp;
144 }
145
146 return str_replace($match['0'], $str, $string);
147 }
adminb0dd10f2006-08-25 17:25:49 +0000148
149 // --------------------------------------------------------------------
150
151 /**
152 * Matches a variable pair
153 *
154 * @access private
155 * @param string
156 * @param string
157 * @return mixed
158 */
159 function _match_pair($string, $variable)
160 {
161 if ( ! preg_match("|".$this->l_delim . $variable . $this->r_delim."(.+)".$this->l_delim . '/' . $variable . $this->r_delim."|s", $string, $match))
162 {
163 return FALSE;
164 }
165
166 return $match;
167 }
adminb0dd10f2006-08-25 17:25:49 +0000168
169}
170// END Parser Class
171?>