Parser.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
  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. * Parser Class
  18. *
  19. * @package CodeIgniter
  20. * @subpackage Libraries
  21. * @category Parser
  22. * @author ExpressionEngine Dev Team
  23. * @link http://codeigniter.com/user_guide/libraries/parser.html
  24. */
  25. class CI_Parser {
  26. var $l_delim = '{';
  27. var $r_delim = '}';
  28. var $object;
  29. /**
  30. * Parse a template
  31. *
  32. * Parses pseudo-variables contained in the specified template view,
  33. * replacing them with the data in the second param
  34. *
  35. * @access public
  36. * @param string
  37. * @param array
  38. * @param bool
  39. * @return string
  40. */
  41. public function parse($template, $data, $return = FALSE)
  42. {
  43. $CI =& get_instance();
  44. $template = $CI->load->view($template, $data, TRUE);
  45. return $this->_parse($template, $data, $return);
  46. }
  47. // --------------------------------------------------------------------
  48. /**
  49. * Parse a String
  50. *
  51. * Parses pseudo-variables contained in the specified string,
  52. * replacing them with the data in the second param
  53. *
  54. * @access public
  55. * @param string
  56. * @param array
  57. * @param bool
  58. * @return string
  59. */
  60. function parse_string($template, $data, $return = FALSE)
  61. {
  62. return $this->_parse($template, $data, $return);
  63. }
  64. // --------------------------------------------------------------------
  65. /**
  66. * Parse a template
  67. *
  68. * Parses pseudo-variables contained in the specified template,
  69. * replacing them with the data in the second param
  70. *
  71. * @access public
  72. * @param string
  73. * @param array
  74. * @param bool
  75. * @return string
  76. */
  77. function _parse($template, $data, $return = FALSE)
  78. {
  79. if ($template == '')
  80. {
  81. return FALSE;
  82. }
  83. foreach ($data as $key => $val)
  84. {
  85. if (is_array($val))
  86. {
  87. $template = $this->_parse_pair($key, $val, $template);
  88. }
  89. else
  90. {
  91. $template = $this->_parse_single($key, (string)$val, $template);
  92. }
  93. }
  94. if ($return == FALSE)
  95. {
  96. $CI =& get_instance();
  97. $CI->output->append_output($template);
  98. }
  99. return $template;
  100. }
  101. // --------------------------------------------------------------------
  102. /**
  103. * Set the left/right variable delimiters
  104. *
  105. * @access public
  106. * @param string
  107. * @param string
  108. * @return void
  109. */
  110. function set_delimiters($l = '{', $r = '}')
  111. {
  112. $this->l_delim = $l;
  113. $this->r_delim = $r;
  114. }
  115. // --------------------------------------------------------------------
  116. /**
  117. * Parse a single key/value
  118. *
  119. * @access private
  120. * @param string
  121. * @param string
  122. * @param string
  123. * @return string
  124. */
  125. function _parse_single($key, $val, $string)
  126. {
  127. return str_replace($this->l_delim.$key.$this->r_delim, $val, $string);
  128. }
  129. // --------------------------------------------------------------------
  130. /**
  131. * Parse a tag pair
  132. *
  133. * Parses tag pairs: {some_tag} string... {/some_tag}
  134. *
  135. * @access private
  136. * @param string
  137. * @param array
  138. * @param string
  139. * @return string
  140. */
  141. function _parse_pair($variable, $data, $string)
  142. {
  143. if (FALSE === ($match = $this->_match_pair($string, $variable)))
  144. {
  145. return $string;
  146. }
  147. $str = '';
  148. foreach ($data as $row)
  149. {
  150. $temp = $match['1'];
  151. foreach ($row as $key => $val)
  152. {
  153. if ( ! is_array($val))
  154. {
  155. $temp = $this->_parse_single($key, $val, $temp);
  156. }
  157. else
  158. {
  159. $temp = $this->_parse_pair($key, $val, $temp);
  160. }
  161. }
  162. $str .= $temp;
  163. }
  164. return str_replace($match['0'], $str, $string);
  165. }
  166. // --------------------------------------------------------------------
  167. /**
  168. * Matches a variable pair
  169. *
  170. * @access private
  171. * @param string
  172. * @param string
  173. * @return mixed
  174. */
  175. function _match_pair($string, $variable)
  176. {
  177. 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))
  178. {
  179. return FALSE;
  180. }
  181. return $match;
  182. }
  183. }
  184. // END Parser Class
  185. /* End of file Parser.php */
  186. /* Location: ./system/libraries/Parser.php */