Parser.php 4.4 KB

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