I was wondering how do I go about on creating a template engine. Something like the XMB forum board templates (I really like how the templates are handled on the XMB forum board script). Something that will store the template in a database table and called out with the eval(). Please do not recommend smarty. For some reason, I'm not gonna go with smarty.
Templating in PHP
gilizama wrote:I was wondering how do I go about on creating a template engine. Something like the XMB forum board templates (I really like how the templates are handled on the XMB forum board script). Something that will store the template in a database table and called out with the eval(). Please do not recommend smarty. For some reason, I'm not gonna go with smarty.
Yes! Someone else that doesn't like smarty! I'm not alone!
I have a working system like this, and it's really not that hard. Just make a class, and in the class, have functions that load templates, then somehow compile them (I did a ton of preg_replace() functions), and echo your output.
FatalError wrote:Yes! Someone else that doesn't like smarty! I'm not alone!
I have a working system like this, and it's really not that hard. Just make a class, and in the class, have functions that load templates, then somehow compile them (I did a ton of preg_replace() functions), and echo your output.
do you mind showing me or giving me a quick code examp or something. im pretty new to php. in not a stranger to it, but at the same time im not ALL that familiar with it.
I don't like to show people my code while it's still in Beta, so I won't post it (sorry). But I'll give you an explanation of what I did.
I have a class (called template). I have the following functions:
load_template: Loads a template and saves the contents of the template file in a variable.
assign_vars: Assigns variables onto the template. For example, if you compare it to phpBB's template engine, it's the same thing as assign_block_vars.
parse: This is the function I call to compile the whole thing, and all it does is calls all the nessessary functions and echoes the output.
compile: The big function. This does everything that makes it a template engine. First, I used a bunch of preg_replace functions to find and replace if, else and loop statements in the template. Then it breaks the code up into lines and turns the needed lines into PHP code, which is eval()'d.