Parser.php
Go to the documentation of this file.00001 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 class CI_Parser {
00028
00029 var $l_delim = '{';
00030 var $r_delim = '}';
00031 var $object;
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 function parse($template, $data, $return = FALSE)
00046 {
00047 $CI =& get_instance();
00048 $template = $CI->load->view($template, $data, TRUE);
00049
00050 if ($template == '')
00051 {
00052 return FALSE;
00053 }
00054
00055 foreach ($data as $key => $val)
00056 {
00057 if (is_array($val))
00058 {
00059 $template = $this->_parse_pair($key, $val, $template);
00060 }
00061 else
00062 {
00063 $template = $this->_parse_single($key, (string)$val, $template);
00064 }
00065 }
00066
00067 if ($return == FALSE)
00068 {
00069 $CI->output->final_output = $template;
00070 }
00071
00072 return $template;
00073 }
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 function set_delimiters($l = '{', $r = '}')
00086 {
00087 $this->l_delim = $l;
00088 $this->r_delim = $r;
00089 }
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102 function _parse_single($key, $val, $string)
00103 {
00104 return str_replace($this->l_delim.$key.$this->r_delim, $val, $string);
00105 }
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120 function _parse_pair($variable, $data, $string)
00121 {
00122 if (FALSE === ($match = $this->_match_pair($string, $variable)))
00123 {
00124 return $string;
00125 }
00126
00127 $str = '';
00128 foreach ($data as $row)
00129 {
00130 $temp = $match['1'];
00131 foreach ($row as $key => $val)
00132 {
00133 if ( ! is_array($val))
00134 {
00135 $temp = $this->_parse_single($key, $val, $temp);
00136 }
00137 else
00138 {
00139 $temp = $this->_parse_pair($key, $val, $temp);
00140 }
00141 }
00142
00143 $str .= $temp;
00144 }
00145
00146 return str_replace($match['0'], $str, $string);
00147 }
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159 function _match_pair($string, $variable)
00160 {
00161 if ( ! preg_match("|".$this->l_delim . $variable . $this->r_delim."(.+)".$this->l_delim . '/' . $variable . $this->r_delim."|s", $string, $match))
00162 {
00163 return FALSE;
00164 }
00165
00166 return $match;
00167 }
00168
00169 }
00170
00171
00172
00173