Welcome Guest, Not a member yet? Register   Sign In
[Library] Simple Dwoo implementation
#1

[eluser]Phil Sturgeon[/eluser]
Dwoo is a simple templating engine similar to Smarty but written entirky in PHP 5 OOP, making it much quicker and lighter.

There is already a implementation for CodeIgniter packaged with the Dwoo download which is pretty sweet, but I am very against the way in which it was done. I don't want to write $this->dwootemplate->load() or anything similar, as if I change parsers (which I have done once or twice) then ALL of my controllers need to change and that's no fun at all.

Instead I decided that extending and replacing the Parser library logic was a better idea, so here is the code to get Dwoo working for you. You of course need to download and install Dwoo, this is just the wrapper library.

It all works EXACTLY the same way as views/parser does in core CodeIgniter, with the addition of adding string_parse() which accepts a HTML string instead of the "view" name.

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* Parser Class
*
* @package        CodeIgniter
* @subpackage        Libraries
* @category        Parser
* @author        Phil Sturgeon
*/
include(APPPATH.'libraries/dwoo/dwooAutoload.php');

class MY_Parser extends CI_Parser {
    
    private $CI;
    
    function __construct()
    {
         $this->CI =& get_instance();
        
        $this->CI->config->load('parser', TRUE);
        $this->config = $this->CI->config->item('parser');
    }
    
    /**
     *  Parse a view file
     *
     * 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)
    {
        $string = $this->CI->load->view($template, $data, TRUE);
        
        return $this->_parse($string, $data, $return);    
    }
    
    /**
     *  String parse
     *
     * Parses pseudo-variables contained in the string content,
     * replacing them with the data in the second param
     *
     * @access    public
     * @param    string
     * @param    array
     * @param    bool
     * @return    string
     */
    function string_parse($string, $data = NULL, $return = FALSE)
    {
        return $this->_parse($string, $data, $return);
    }
    
    /**
     *  Parse
     *
     * 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($string, $data, $return = FALSE)
    {
        // Start benchmark
        $this->CI->benchmark->mark('dwoo_parse_start');

        // Main Dwoo object
        $dwoo = new Dwoo();

         // The directory where compiled templates are located
        $dwoo->setCompileDir($this->config['parser_compile_dir']);
        $dwoo->setCacheDir($this->config['parser_cache_dir']);
        $dwoo->setCacheTime($this->config['parser_cache_time']);
        
        // Object containing data
        $dwoo_data = new Dwoo_Data();
        
        // Convert from object to array
        if(is_object($data))
        {
            $data = (array) $data;
        }
        
        // If not an empty array, set data
        if(is_array($data) && $data !== array())
        {
            $dwoo_data->setData($data);
        }
        
        try
        {
            // Object of the template
            $tpl = new Dwoo_Template_String($string);
            
            // render the template
            $parsed_string = $dwoo->get($tpl, $dwoo_data);
        }
        
        catch(Dwoo_Compilation_Exception $e)
        {
            show_error($e);
        }
        
        // Finish benchmark
        $this->CI->benchmark->mark('dwoo_parse_end');

        // Return results or not ?
        if ($return == FALSE)
        {
            $this->CI->output->append_output($parsed_string);
        }
        
        return $parsed_string;
    }
    
    // --------------------------------------------------------------------
    
}
// END MY_Parser Class

/* End of file MY_Parser.php */
/* Location: ./application/libraries/MY_Parser.php */

Enjoy folks.


Messages In This Thread
[Library] Simple Dwoo implementation - by El Forum - 11-15-2009, 10:11 AM
[Library] Simple Dwoo implementation - by El Forum - 11-17-2009, 07:50 AM
[Library] Simple Dwoo implementation - by El Forum - 11-17-2009, 08:07 AM
[Library] Simple Dwoo implementation - by El Forum - 11-23-2009, 04:19 PM
[Library] Simple Dwoo implementation - by El Forum - 11-28-2009, 05:21 AM
[Library] Simple Dwoo implementation - by El Forum - 11-28-2009, 07:51 AM
[Library] Simple Dwoo implementation - by El Forum - 11-28-2009, 09:31 AM
[Library] Simple Dwoo implementation - by El Forum - 04-14-2010, 01:06 AM



Theme © iAndrew 2016 - Forum software by © MyBB