Give CodeIgniter's Parser library more kick with Dwoo

Posted: 2009-11-23
Category: CodeIgniter

Anyone who has used the CodeIgniter Parser library will agree it is damn basic. The library is litterally just a way to use variables and foreach loops in your views without using PHP syntax. I have never found that too helpful, and when I started looking for a way to enable a template parser for the PyroCMS page manager, what I ended up with was HelpfulParser. That allowed us to use helpers and other PHP functions, but was also pretty basic.

Not wanting to spend too much time re-inventing the wheel I had a look at existing templating engines and decided to use Dwoo. It is a lightwight, PHP 5 only templating engine intended as a replacement for Smarty. The main reason I picked Dwoo over another alternative like Twig was the syntax. If BDU's are going to using this, it needs to be as easy as possible to learn.

Suprisingly the Dwoo download came witha CodeIgniter implementation already, bundled as a very simple wrapper library with syntax such as $this->dwootemplate->assign('foo', 'bar') and $this->dwootemplate->display(). I don't you about you folks, but I hate putting the name of a third-party anything in my code when it can be avoided.

So after a little head-scratching, we now have CodeIgniter-Dwoo. DwooParser? DwooIgniter? Whatever we call it, my new Dwoo implementation for CodeIgniter sits in with "the CodeIgniter way" perfectly and the only code you will need to change within your application is the Parser file and your view files. No changes to your controllers at all!

The Lead Developer of Dwoo contacted me about this implementation and it may way well be included in the Dwoo 1.2 release in place of the old one, so please test this out and let me know if you have any issues or suggestions.

Dwoo implementation for CodeIgniter

Comments

Gravatar
Nogga

2010-08-05

I just downloaded your wrapper and it is working like a charm. I would like to use it with forms, but actually do not know how to get the repopulation functions to run.

Any advice?

Gravatar
Eyus

2010-12-24

Hi Phil, can I save dwoo template script (variable, conditional, looping) into database? If yes, how to parse it from controller. I want user create their template from admin panel and save all template script into the database. Thank you for your time, you're the expert...

Gravatar

2010-12-24

Sure Eyus that's actually why I created this library.

$page->content = $this->parser->parse_string($page->content, $data);

$data['page'] = $page;

$this->load->view('page', $data)

Gravatar
Tisho

2011-02-11

Hi Phil,
Great work. Thank you for your time and writing all that stuff.
I just upgraded to CI 2.0. Began from clean install and HMVC extensions, Template library, and copied all the modules from 1.7.2 and made all the configuration files.

But unfortunately, the parser seems to not working. Any ideas regarding the upgrade

Gravatar

2011-02-11

I have no idea what "not working" means, so can't really help you there.

Gravatar
Tisho

2011-02-11

Well, consider the following situation.

// controller
$this->template->set('data', 'blah blah blah');
$this->template->build('tpl');

// view tpl
{$data}

It displays "{$data}", and not "blah blah blah"

The parser itself does not work

Gravatar
Tisho

2011-02-11

I fixed it. Never mind.

The My_Parser.php file should remain in the application/libraries folder!
I moved it to application/core folder

Gravatar
Manik

2011-04-06

Hi Phil,
i installed CI 2.0.1 along with HMVC and then template library. when i tested dwoo_test is shows
Fatal error: Cannot access protected property CI_Output::$parse_exec_vars in D:\wamp\www\CI\application\libraries\MY_Parser.php on line 158

Gravatar
Manik

2011-04-06

it shoes error in this part :
if (CI_VERSION < 2 OR $this->_ci->output->parse_exec_vars === TRUE)
{
$memory = ( ! function_exists('memory_get_usage')) ? '0' : round(memory_get_usage() / 1024 / 1024, 2) . 'MB';

$string = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsed, $memory), $string);
}

Gravatar
Michel

2011-04-16

YES !!

The My_Parser.php file should remain in the application/libraries folder!
I moved it to application/core folder

Gravatar
Gustavo Baldera

2011-07-27

the function "extends" from Dwoo not work for me...it returns the name of the template that Iḿ triying to extends..ie:

//page.tpl
Hello!!

//message.tpl
{extends "page.tpl"}

//controller
$this->parser->parse('message.tpl', array('message' => 'OH HAI!'));

//returns
"page.tpl"

Gravatar

2011-07-27

I don't think I ever said that extends would work.

Posting comments after three months has been disabled.