Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 4.3.2 or newer |
| 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author ExpressionEngine Dev Team |
Derek Jones | 7f3719f | 2010-01-05 13:35:37 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html |
| 11 | * @link http://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 ExpressionEngine Dev Team |
| 25 | * @link http://codeigniter.com/user_guide/libraries/parser.html |
| 26 | */ |
| 27 | class CI_Parser { |
| 28 | |
| 29 | var $l_delim = '{'; |
| 30 | var $r_delim = '}'; |
| 31 | var $object; |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 32 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 33 | /** |
| 34 | * Parse a template |
| 35 | * |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 36 | * Parses pseudo-variables contained in the specified template view, |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | $CI =& get_instance(); |
| 48 | $template = $CI->load->view($template, $data, TRUE); |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 49 | |
| 50 | return $this->_parse($template, $data, $return); |
| 51 | } |
| 52 | |
| 53 | // -------------------------------------------------------------------- |
| 54 | |
| 55 | /** |
| 56 | * Parse a String |
| 57 | * |
| 58 | * Parses pseudo-variables contained in the specified string, |
| 59 | * replacing them with the data in the second param |
| 60 | * |
| 61 | * @access public |
| 62 | * @param string |
| 63 | * @param array |
| 64 | * @param bool |
| 65 | * @return string |
| 66 | */ |
| 67 | function parse_string($template, $data, $return = FALSE) |
| 68 | { |
| 69 | return $this->_parse($template, $data, $return); |
| 70 | } |
| 71 | |
| 72 | // -------------------------------------------------------------------- |
| 73 | |
| 74 | /** |
| 75 | * Parse a template |
| 76 | * |
| 77 | * Parses pseudo-variables contained in the specified template, |
| 78 | * replacing them with the data in the second param |
| 79 | * |
| 80 | * @access public |
| 81 | * @param string |
| 82 | * @param array |
| 83 | * @param bool |
| 84 | * @return string |
| 85 | */ |
| 86 | function _parse($template, $data, $return = FALSE) |
| 87 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 88 | if ($template == '') |
| 89 | { |
| 90 | return FALSE; |
| 91 | } |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 92 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 93 | foreach ($data as $key => $val) |
| 94 | { |
| 95 | if (is_array($val)) |
| 96 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 97 | $template = $this->_parse_pair($key, $val, $template); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 98 | } |
| 99 | else |
| 100 | { |
| 101 | $template = $this->_parse_single($key, (string)$val, $template); |
| 102 | } |
| 103 | } |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 104 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 105 | if ($return == FALSE) |
| 106 | { |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 107 | $CI =& get_instance(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 108 | $CI->output->append_output($template); |
| 109 | } |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 110 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 111 | return $template; |
| 112 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 113 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 114 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 115 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 116 | /** |
| 117 | * Set the left/right variable delimiters |
| 118 | * |
| 119 | * @access public |
| 120 | * @param string |
| 121 | * @param string |
| 122 | * @return void |
| 123 | */ |
| 124 | function set_delimiters($l = '{', $r = '}') |
| 125 | { |
| 126 | $this->l_delim = $l; |
| 127 | $this->r_delim = $r; |
| 128 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 129 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 130 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 131 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 132 | /** |
| 133 | * Parse a single key/value |
| 134 | * |
| 135 | * @access private |
| 136 | * @param string |
| 137 | * @param string |
| 138 | * @param string |
| 139 | * @return string |
| 140 | */ |
| 141 | function _parse_single($key, $val, $string) |
| 142 | { |
| 143 | return str_replace($this->l_delim.$key.$this->r_delim, $val, $string); |
| 144 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 145 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 146 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 147 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 148 | /** |
| 149 | * Parse a tag pair |
| 150 | * |
| 151 | * Parses tag pairs: {some_tag} string... {/some_tag} |
| 152 | * |
| 153 | * @access private |
| 154 | * @param string |
| 155 | * @param array |
| 156 | * @param string |
| 157 | * @return string |
| 158 | */ |
| 159 | function _parse_pair($variable, $data, $string) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 160 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 161 | if (FALSE === ($match = $this->_match_pair($string, $variable))) |
| 162 | { |
| 163 | return $string; |
| 164 | } |
| 165 | |
| 166 | $str = ''; |
| 167 | foreach ($data as $row) |
| 168 | { |
| 169 | $temp = $match['1']; |
| 170 | foreach ($row as $key => $val) |
| 171 | { |
| 172 | if ( ! is_array($val)) |
| 173 | { |
| 174 | $temp = $this->_parse_single($key, $val, $temp); |
| 175 | } |
| 176 | else |
| 177 | { |
| 178 | $temp = $this->_parse_pair($key, $val, $temp); |
| 179 | } |
| 180 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 181 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 182 | $str .= $temp; |
| 183 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 184 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 185 | return str_replace($match['0'], $str, $string); |
| 186 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 187 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 188 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 189 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 190 | /** |
| 191 | * Matches a variable pair |
| 192 | * |
| 193 | * @access private |
| 194 | * @param string |
| 195 | * @param string |
| 196 | * @return mixed |
| 197 | */ |
| 198 | function _match_pair($string, $variable) |
| 199 | { |
Derek Jones | 23e796f | 2010-05-21 12:45:25 -0500 | [diff] [blame] | 200 | if ( ! preg_match("|" . preg_quote($this->l_delim) . $variable . preg_quote($this->r_delim) . "(.+?)". preg_quote($this->l_delim) . '/' . $variable . preg_quote($this->r_delim) . "|s", $string, $match)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 201 | { |
| 202 | return FALSE; |
| 203 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 204 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 205 | return $match; |
| 206 | } |
| 207 | |
| 208 | } |
| 209 | // END Parser Class |
| 210 | |
| 211 | /* End of file Parser.php */ |
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 212 | /* Location: ./system/libraries/Parser.php */ |