I'd like to create a simple template system for no particular reason other than I want to do it. I realize solutions such as Smarty exist, but I'd prefer to create one on my own. Something that is simple but I can expand on it should I want to. What is the best way to do this? I'm not looking for code, but rather a pointer in the right direction of what needs to be done. I've tried before but I've got to be missing something because all of my attempts fail. Thanks.
Simple template system
Collapse
X
-
With my template parser, I use a very simple loop that goes through every character of the template string and checks for matches of the open tag. When one is found, it looks for an <else> or </if> tag. It uses a stack to add any nested <if> tags so it can then balance the </if>s. At the end, when the stack is 0 and there's an </if>, it takes the position of the open tag and the position of the close tag, substr()s it and then processes the data. Finally, it uses str_replace() to replace the original extracted text with the processed data and advances from the previous character position by one place.
If you want to see the actual code for it, download ISSO (my framework) and open up template.php. -
Thanks daemon. However, I'm still a little bit lost as to where to start, including how to set up the database and what kind of code I should be trying to write and how everything should be organized. Could you provide any insight into that?Comment
-
The way I do templates is to just put something like:
Code:eval('$whateverbits .= "' . addslashes(file_get_contents('templatefolder/template.html')) . '";');
If you want to use teh database it will get a little harder with need for caching templates and stuff like that, so create a function for it.
I'm not sure how to do conditions in it, would like to hear some peoples ideas though.Last edited by rossco_2005; Wed 24 May '06, 11:53am.Comment
-
Originally posted by eXaulzWait, do you want a Flat-File (templates are files) template engine or a DB (templatesare table entries) template-ngine?Comment
-
Why you want to use a template engine that uses templates that are stored in a database?
Why not using files?
It's german but the code describes itself .. imho:
--> http://forum.developers-guide.net/showthread.php?t=684
Maybe it helps you?
If not .. why not using Smarty instead? Sure, Smarty is sometimes a little bit toooo large but I do not know what you want to realize.Comment
-
Opening files is actually slower than using a database by up to a second.Comment
-
I want to create my own system just because I can. I know Smarty exists however I do not wish to use it. Any other ideas?Comment
widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
Comment