Welcome Guest, Not a member yet? Register   Sign In
Global Parser Vars
#1

[eluser]ShoeLace1291[/eluser]
I was getting tired of having to parse very common variables throughout every model and view file. So I figured out a way to have the parser library parse global variables based on a config file.

For example: You now no longer have to create parser vars such as IMG_URL every time you load a view file.

application/config/parser.php
Code:
<?php

$config['parser']['IMG_URL'] = 'http://www.mysite.com/images';
$config['parser']['CSS_URL'] = 'http://www.mysite.com/css';
$config['parser']['JS_URL'] = 'http://www.mysite.com/js';

application/libraries/Parser.php
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter
*
* An open source application development framework for PHP 5.1.6 or newer
*
* @package  CodeIgniter
* @author  ExpressionEngine Dev Team
* @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
* @license  http://ellislab.com/codeigniter/user-guide/license.html
* @link  http://codeigniter.com
* @since  Version 1.0
* @filesource
*/

// ------------------------------------------------------------------------

/**
* Parser Class
*
* @package  CodeIgniter
* @subpackage Libraries
* @category Parser
* @author  ExpressionEngine Dev Team
* @link  http://ellislab.com/codeigniter/user-guide/libraries/parser.html
*/
class Parser {

var $l_delim = '{';
var $r_delim = '}';
var $object;

/**
  *  Parse a template
  *
  * Parses pseudo-variables contained in the specified template view,
  * replacing them with the data in the second param
  *
  * @access public
  * @param string
  * @param array
  * @param bool
  * @return string
  */
public function parse($template, $data, $return = FALSE)
{
  $CI =& get_instance();
  $template = $CI->load->view($template, $data, TRUE);

                /* Global Parser Variables */
                $CI->config->load('parser');
                $config = $CI->config->item('parser');
  
                foreach($config as $key => $val){
                    
                    $data[$key] = $val;
                    
                }
                
  return $this->_parse($template, $data, $return);
}

// --------------------------------------------------------------------

/**
  *  Parse a String
  *
  * Parses pseudo-variables contained in the specified string,
  * replacing them with the data in the second param
  *
  * @access public
  * @param string
  * @param array
  * @param bool
  * @return string
  */
function parse_string($template, $data, $return = FALSE)
{
  return $this->_parse($template, $data, $return);
}

// --------------------------------------------------------------------

/**
  *  Parse a template
  *
  * Parses pseudo-variables contained in the specified template,
  * replacing them with the data in the second param
  *
  * @access public
  * @param string
  * @param array
  * @param bool
  * @return string
  */
function _parse($template, $data, $return = FALSE)
{
  if ($template == '')
  {
   return FALSE;
  }

  foreach ($data as $key => $val)
  {
   if (is_array($val))
   {
    $template = $this->_parse_pair($key, $val, $template);
   }
   else
   {
    $template = $this->_parse_single($key, (string)$val, $template);
   }
  }

  if ($return == FALSE)
  {
   $CI =& get_instance();
   $CI->output->append_output($template);
  }

  return $template;
}

// --------------------------------------------------------------------

/**
  *  Set the left/right variable delimiters
  *
  * @access public
  * @param string
  * @param string
  * @return void
  */
function set_delimiters($l = '{', $r = '}')
{
  $this->l_delim = $l;
  $this->r_delim = $r;
}

// --------------------------------------------------------------------

/**
  *  Parse a single key/value
  *
  * @access private
  * @param string
  * @param string
  * @param string
  * @return string
  */
function _parse_single($key, $val, $string)
{
  return str_replace($this->l_delim.$key.$this->r_delim, $val, $string);
}

// --------------------------------------------------------------------

/**
  *  Parse a tag pair
  *
  * Parses tag pairs:  {some_tag} string... {/some_tag}
  *
  * @access private
  * @param string
  * @param array
  * @param string
  * @return string
  */
function _parse_pair($variable, $data, $string)
{
  if (FALSE === ($match = $this->_match_pair($string, $variable)))
  {
   return $string;
  }

  $str = '';
  foreach ($data as $row)
  {
   $temp = $match['1'];
   foreach ($row as $key => $val)
   {
    if ( ! is_array($val))
    {
     $temp = $this->_parse_single($key, $val, $temp);
    }
    else
    {
     $temp = $this->_parse_pair($key, $val, $temp);
    }
   }

   $str .= $temp;
  }

  return str_replace($match['0'], $str, $string);
}

// --------------------------------------------------------------------

/**
  *  Matches a variable pair
  *
  * @access private
  * @param string
  * @param string
  * @return mixed
  */
function _match_pair($string, $variable)
{
  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))
  {
   return FALSE;
  }

  return $match;
}

}
// END Parser Class

/* End of file Parser.php */
/* Location: ./system/libraries/Parser.php */




Theme © iAndrew 2016 - Forum software by © MyBB