Parser.php

Go to the documentation of this file.
00001 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002 /**
00003  * CodeIgniter
00004  *
00005  * An open source application development framework for PHP 4.3.2 or newer
00006  *
00007  * @package             CodeIgniter
00008  * @author              ExpressionEngine Dev Team
00009  * @copyright   Copyright (c) 2006, EllisLab, Inc.
00010  * @license             http://codeigniter.com/user_guide/license.html
00011  * @link                http://codeigniter.com
00012  * @since               Version 1.0
00013  * @filesource
00014  */
00015 
00016 // ------------------------------------------------------------------------
00017 
00018 /**
00019  * Parser Class
00020  *
00021  * @package             CodeIgniter
00022  * @subpackage  Libraries
00023  * @category    Parser
00024  * @author              ExpressionEngine Dev Team
00025  * @link                http://codeigniter.com/user_guide/libraries/parser.html
00026  */
00027 class CI_Parser {
00028 
00029         var $l_delim = '{';
00030         var $r_delim = '}';
00031         var $object;
00032                 
00033         /**
00034          *  Parse a template
00035          *
00036          * Parses pseudo-variables contained in the specified template,
00037          * replacing them with the data in the second param
00038          *
00039          * @access      public
00040          * @param       string
00041          * @param       array
00042          * @param       bool
00043          * @return      string
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          *  Set the left/right variable delimiters
00079          *
00080          * @access      public
00081          * @param       string
00082          * @param       string
00083          * @return      void
00084          */
00085         function set_delimiters($l = '{', $r = '}')
00086         {
00087                 $this->l_delim = $l;
00088                 $this->r_delim = $r;
00089         }
00090         
00091         // --------------------------------------------------------------------
00092         
00093         /**
00094          *  Parse a single key/value
00095          *
00096          * @access      private
00097          * @param       string
00098          * @param       string
00099          * @param       string
00100          * @return      string
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          *  Parse a tag pair
00111          *
00112          * Parses tag pairs:  {some_tag} string... {/some_tag}
00113          *
00114          * @access      private
00115          * @param       string
00116          * @param       array
00117          * @param       string
00118          * @return      string
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          *  Matches a variable pair
00153          *
00154          * @access      private
00155          * @param       string
00156          * @param       string
00157          * @return      mixed
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 // END Parser Class
00171 
00172 /* End of file Parser.php */
00173 /* Location: ./system/libraries/Parser.php */