Building a Template Parser Class with PHP, Part I (
Page 1 of 5 ) It is easy to create a templating system in PHP; in fact, there are a number of templating system packages. But what if you're putting together a relatively small website, and don't really need one of those full-fledged systems? In this first part of a two-part article, you will learn how to create a simple but extensible PHP class for parsing templates.Introduction
Without a doubt, PHP is one of the most popular languages currently available for fast Web development. Its extreme ease and flexibility has made it a great source for creating a wide gamut of applications, well suited for satisfying the needs of different projects. Certainly, one of the most common applications that have grown noticeably in popularity is what is widely know as a templating system.
The question is: what is a templating system? Well, in short terms, it’s an easy way to create large sites with some effort to maintain a consistent appearance through the different documents. Just by having only one template file, which stores the HTML markup and the data placeholders, we are able to manipulate it with a PHP script that takes the template file and replaces those placeholders with the corresponding content, in order to generate the final version of the page. The concept behind templating is very handy, since it allows us to separate the markup and visual presentation from the logic of the application in different layers.
As a PHP programmer, you probably have used some of the most popular template system packages, including the powerful Smarty, or other solutions such as Fast Template or Pat Template among others (add your own to the list). Most of these packages are full-featured and mature solutions designed to be generally implemented on medium and large size projects. They present numerous options that sometimes are pretty cumbersome, particularly when dealing with small projects, where templating needs are rather basic.
Okay, right now I can hear some well-intended programmers complaining about this, arguing that those packages can be perfectly implemented for the tiny lovely pet website of Uncle Johnny, including a huge Microsoft SQL Server database system, and so on. You get the idea. But, let’s be honest. For small applications we need a simple templating system that nicely fits our needs.
Because of the reasons I mentioned above, I decided to write on my own a simple but extensible PHP class for parsing templates. I designed it to satisfy the minor requirements of small websites. Fine, are you ready to put our hands on the code? Just follow me. We’re involved in this situation together!
| | Discuss Building a Template Parser Class with PHP, Part I | | | | | | | Definitively, building a small template system is easy with PHP. Hopefully... | | | | | | True... the article showed how easy it is to create sucha sistem..howevere these... | | | | | | Since the class was introduced as a simple template parser, the addition of new... | | | | | | The only things I see an immediate need for are a way to handle repeating fields... | | | | | | Unfortunately, those possible additions are not implemented in part II. I only... | | | | | | While I appreciate your approach, I can see an issue. I understand what I want it... | | | | | | Hello friend,
I get your point about a template system. It's truly desirable... | | | | | | Yes, if I could only get the swedish characters å ä ö Å Ä Ö to be displayed... | | | | | |
According to your needs, you should include the charset ISO-8859-1 into the head... | | | | | | Thanks for the fast reply!! :-)
I added the meta-tag. And I also added some code,... | | | | | | I'm glad you found a solution for the problem.
Just one advice: whenever you can,... | | | | | | one comment: mixing html and php is not a problem!!! replacing <?= $var => by {$var}... | | | | | |
Thank you for pointing that nice article. Sure it's very valuable for any web... | | | | | | Wonderful simple looking template parser; Your explanation is very understandable,... | | | | | | Hi friend,
I'm glad you liked the article. Of course I've no problem sending some... | | | | | | I was wondering how you would use this template system when you want different... | | | | | | It's easy to create different pages with the same template file. This way, you're... | | | | | | It's easy to create different pages with the same template file. This way, you're... | | | | | | Im fairly new to PHP and only know the basics of it. Im not sure how this could be... | | | | | | Yes, I see your point. Here´s a possible solution.
First, perform your queries, and... | | | | | | Yes thats what I want to do but as you said.. build it into a variable for example... | | | | | | Hello, here's your solution:
// initialize your output... | | | | | | I´m really glad I´ve been monitoring this discussion. This while-loop is just what... | | | | | | Hey, I'm glad you found it useful for your needs.
Thank you for the kind words and... | | | | | | Just learning about classes. I tried to use your code, but it didn't work. I did a... | | | | | | Hello, I found the error in your code. You listed the following:
foreach($tags as... | | | | | | Stunning class first of all. Very clear, very helpful and very practical.
Now the... | | | | | | Thank you for giving your opinions about the class.
Regarding your question, it´s... | | | | | | Thanks for getting back, so swifthly too.
I agree that adding the conditionals to... | | | | | | Forgot to add:
The template is thus:
{header}
{login-box}
{leftbox} | | | | | | Thank you for the kind words.
I think this is what you're looking for:
First,... | | | | | | Perfect solution.
Thanks again.
Like many others it seems, I'd love to see a... | | | | | | Nice to hear it was the solution for your problem.
About Part III, I hope to find... | | | | | | All going really well. Then I added a template which includes:
if... | | | | | | Hello friend,
I hope this helps. In first place when we're talking about a... | | | | | | Damn. You've pretty much said what I was hoping you wouldn't -- that being that it... | | | | | | Oddly, I've just noticed that if I reinstantiate the class within the tpl-footer... | | | | | | I guess I found the error. I will try to keep it short, so spiders won't index much... | | | | | | No, sadly that's not the answer. I should have perhaps mentioned, the init.inc.php... | | | | | | hi everybody
how i can build my database by php parsing . i mean that i wanna... | | | | | | There are several ways to update your database, even in that given short time. One... | | | | | | Hi there could the author possibly email me at liamdawe@gmail.com and help me out a... | | | | | | Hello,
I've emailed you at the given address. Thanks! | | | | | | is it any way i can get info from mysql into a {something}? like:
'cat' => ... | | | | | | About your question, I think that the best way to include query resources is first... | | | | | | thank you!
now i have another problem :(
it works when i just print out 2 things... | | | | | | Generally, and depending on the configuration file, PHP access files by using the... | | | | | | Very nice class indeed, simple, basic and yet very powerfull!
However, i`m trying... | | | | | | Thanks for the comments on the class. Now, let's point to your... | | | | | | Thank you for the quick reaction, and also the good example.
I think I can match... | | | | | | You're welcome. I'm glad to know the answer was helpful.
Regards. | | | | | | i`ve extended the class with the option to parse {img}{/img} tags too, using the... | | | | | | Nice class improvement. Adding image-parsing capabilities to the class' logic is a... | | | | | | Another contribution for your nice class, the option to make hyperlinks out of tags,... | | | | | | Excellent implementation of regular expressions. Also, trough regexp, another... | | | | | | I wanted to be able to also parse functions and function arguments with the same... | | | | | | Thank you for commenting on my PHP Template parser class article. I see that you... | | | | | | Hello, I am trying to loop through a template file like so :
There is two... | | | | | | Thanks for the kind comments on my PHP article. In a case like this, where the... | | | | | | >>> Post your comment now! | | | | | |
|
 |
|