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.
#2

[eluser]Enorog[/eluser]
Thank you, this is a much better implementation indeed.

I've been using a simple extended parser so far (I think that was yours as well - you could trigger a helper function with parameters inside curly quotes, but I would like something full featured now (with conditionals for instance).

I do have a question (and perhaps a helpful way on how to organize your parsing). This is how I've got things set up now:

Controller
(calls model, fills $page_info array with stuff, then parses the view like this)
Code:
// PARSE VIEW FOR CMS PLUGINS
$this->parser->parse('page_view', $page_info);

Main view (page_view)
Code:
<?php $this->load->view('page/header'); ?>
<?php if ($view == 'edit') { $this->load->view('page/content_admin'); } ?>
<?php $this->load->view('page/content'); ?>
<?php $this->load->view('page/footer'); ?>

Inside "page/content" view, there is a html string coming from database (stored in $page_info['content'] and inside this html, I call a helper with this parse-ready string
Code:
{helpername:prepare_gallery('folder_name')}


I've done benchmarking for this nested-view approach and I don't see a problem with it, but I'd still be interested if I'm breaking any rules here (output buffering wise). Especially if I parse the whole nested-view thing.

My main question is - should I go to Dwoo if I want more options in my {parser calls}? I wouldn't have to change my controllers, since I already use the parser to call my view, right?
#3

[eluser]Phil Sturgeon[/eluser]
Exactly, this implementation is intended to keep parser logic to the parser and the parseables (yeah I know that's not a word but it should be!).

As for using parsers, load the helper files somewhere (autoload or page controller) and you can use the helper functions like:

Quote:{anchor("foo", "bar")}

A very slightly different syntax from HelpfulParser.
#4

[eluser]Phil Sturgeon[/eluser]
Blogged: Give CodeIgniter's Parser library more kick with Dwoo
#5

[eluser]Enorog[/eluser]
Thanks, great implementation, I replaced HelpfulParser with Dwoo - had it up and running within a minute.

But now I'm thinking - do I need anything more than HelpfulParser? Since I build and design my websites, there is no "designer" I need to accomodate. The template parsing is strictly for my TinyMCE-editable (die, FCKEditor support, die) content area - for clients who are barely able to handle Word. For example, I give them an extra TinyMCE button "Insert Gallery", which pops up a custom dropdown "select gallery name" and this inserts {my_helper:insert_gallery['which_one']}. Some clients like to edit this {stuff} and for them the syntax is cumbersome.

Here's my pros and cons list:

The benefits of HelpfulParser
- access to $CI object in helper, access to loaded objects in CI (Stuff::MyFunction())
- don't need to build Dwoo plugins, my helper file has all my 'plugins' (cleaner directory structure)
- don't want to allow clients access to all variables sent to my view (with helpers, they can only get what the helper provides)

The benefits of DWOO
- shorter, nicer syntax (that's a big one!)
- better caching
- some stuff I don't know I need yet but I'm sure i will soon

Is there anything from "Benefits of HelpfulParser" that I could have with Dwoo too? I really need the $CI object in plugins and filtering available variables.
#6

[eluser]Phil Sturgeon[/eluser]
I'd like to start this by saying "Fuck knows"! HelpfulParser and Dwoo to me are different things for different jobs, even though they seem similar. I would assume you wanted to use my HelpfulParser over Dwoo for the same reason you chose not to use Smarty, as Smarty is pretty similar to Dwoo.

To explain, I never really wanted to use Parser for my view files. My core module files will always use PHP and for me thats fine as only developers should be working on modules. I started HelpfulParser to give PyroCMS users a way to do dynamic stuff in TinyMCE editable text boxes much like yourself, and later on for theme views. HelpfulParser was fine for that job. I made a few helpers like {page_anchor[20|"Some Page"]} and at the time I didnt need much more.

Then I started thinking about if, foreach and more advanced plugins much like EE. While I could easily have extended HelpfulParser even more to support this syntax, I really hate re-inventing perfectly good wheels. As I said I tried out Twig and didnt like it, Dwoo does the job. Plugins and blocks in Dwoo are something I havent had a chance to look into, but I would have thought it would support get_instance() fine. Think about it, helpers are just included PHP files, as are plguins or blocks withing Dwoo, so THEORETICALLY it should work fine.

If HelpfulParser does the job for you: great, i'm glad my random experiement was useful to you, but if you need more I would experiment with CI in Dwoo plugins.
#7

[eluser]Enorog[/eluser]
All good points. I was too lazy to even check if $CI =& get_instance(); would work in a Dwoo plugin, theoretically it should.

And actually I posted my question mostly to hear myself think - which I did and realized I don't need anything more than HelpfulParser right now. My motivation for Dwoo were also mostly nested statements, but I don't need those yet. And if I do, I'd still sooner add that functionality to HP (nicely coded, btw) than having to include a whole library.

Smarty/Dwoo is positioned somewhere between the CI coder+designer (who has nested views and doesn't need templating that S/D offers) and the most basic user (who can only click a TinyMCE button). It's useful for coders who have to deal with designers with an aversion to raw php.

I want my CMS to always remain clean and up to (my) standards, so I don't want to rely on templating, I'll just keep it all in nested views and regular php code.

In the past year, I slowly replaced each of the pre-made CI addons with my own libraries (static classes, actually). The only ones still standing are HelpfulParser and Doctrine ORM. Love them both. Thanks and keep up the good work, Phil.
#8

[eluser]DW@Van [/eluser]
Thanks for the great implementation. My little CMS project is using your Template, Module, and Dwoo library.

I am having a problem to load a view which contains jQuery code. The Dwoo throws an exception. I thought by setting enable_parser to false. The parser will not parse the loading view. Have anyone had this problem?

Currently, I wrapped my javascript block with {literal}{/literal} to keep Dwoo happy. Is there any way to fully disable the parser from the controller?




Theme © iAndrew 2016 - Forum software by © MyBB