TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Templating class (http://www.talkphp.com/advanced-php-programming/1761-templating-class.html)

xenon 12-17-2007 10:04 PM

Templating class
 
Alright, I'm struggling to get a template class (smarty like) going on right here, and I've made it, up to some point. Simple vars, like $a, $some_var, $a_v4r and such are correctly parsed by this simple rule: (variables will look like {$this_is_a_var} in the HTML code)

Code:

$this->output = preg_replace( "/\{\$([a-zA-Z0-9_\-]+?)\}/", '$this->vars[\'\\1\']', $this->output );
But, I cannot replace arrays, for some reason. If I try the php syntax, like {$array['index1']['index2']}, the script is complaining about unescaped quotes and stuff like that. I've managed to get a way working (the smarty way - $arr.index1.index2), but still won't work right. So far I've managed to do this (but notice, however, that only the last part of the array is captured - e.g.: $array['a'][0] would output $array[0], no matter how many keys I add there):

Code:

$this->output = preg_replace( "/\{\$([a-zA-Z0-9_\-]+?)(\['?(?:[a-zA-Z0-9]+?)'?\])*\}/e", '$this->vars[\'\\1\']\\2', $this->output );
I store these template vars into an array, component of a class, like so:

Code:

$this->template_vars = array( 'test_var' => 'some sample var',
'array' => array( 'a' => 'test', 1, 2, 3 ) );

Please help me out with some tips, or a direction, anything. Thanks in advance.

Also, if someone has a little free time & knowledge, please try to respond to the last question of this topic: http://www.talkphp.com/showthread.php?t=1708 It would help me alot. Thanks.

dzysyak 12-29-2007 05:41 PM

I can suggest to use php as a template engine. Actually that is reason why php was created. And it works much faster than any template engine.

xenon 12-29-2007 07:15 PM

Quote:

Originally Posted by dzysyak (Post 7285)
I can suggest to use php as a template engine. Actually that is reason why php was created. And it works much faster than any template engine.

I've already chosen that instead of what I was trying to do. It's a choice that fits my needs much more better than a separate templating engine (which has many limitations). I used output buffering combined with a very simple class and a few includes to achieve that and it works like a charm. Also, I can take full advantage of PHP, without having to replicate its default functions in any way. But thanks anyways.


All times are GMT. The time now is 12:52 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0