Template Engine Question

I’ve been working with PHP for a few months now, and one thing that was brought up to me that I should learn is how to make a template engine. I know there are things like Smarty out there for this, but given I’m trying to improve my own coding ability I felt I should take the time to learn it.

For the most part, I’ve gotten it down. However, there’s one thing I’ve not been able to figure out that I’ve seen done: how to use a piece of a template file repeatedly on a single page.

I know this function is doable, as I’ve been trying to use phpBB as a basis for my learning and THEY do this when you’re viewing a topic: all the posts use a small portion of the viewtopic_body.html template file over and over for each post. What I’m trying to figure it is how I can do this as well. I’ve spent a while now reading over their files, following from one function to the other and trying to find the right function, but I’ve not found what I need (Or if I have, I was too confused by it to recognize it).

Can anyone give me an idea of how I can do this?

hytrain,

I’m not to familiar with the phpBB code, I have however installed it a few times. They have a really good community and a strong following. Everyone template systems are a little different. You should pose your question to their community, which is located here: https://www.phpbb.com/community/

I’m sure someone there will be able to give you a complete summary of how it works.

Hi,

Normally I wouldn’t send people to a tutorial off the site but there is a lot to know and think about when building a template system. A good place to start is http://www.broculos.net/2008/03/how-to-make-simple-html-template-engine.html?m=1#.UhpQTqNOnFoere

It goes into decent detail on how to build your own template system. Its still pretty limited but it’ll get you on the right track. There are also template systems like smarty you’re able to use. Even if you decide to use something like smarty I suggest building a basic template system for yourself just to understand the concepts of how they work so need be you’ll have a better chance of building custom functionality to it if needed later on.

That’s a pretty good site, thanks for sharing. If you ever feel the desire to put together a tutorial like that I can get it added to the tutorials on phphelp.

I may be writing some tutorials in the future. I just have to find the time to be motivated to try and explain the things I’ve learned so far.

One things I’ve learned from my relatively short time actually developing is this is something that you want to be constantly learning something new with and talking with people about. It’ll help you become a stronger developer. However in order for that to work you have to really try to pass on the knowledge you have as well.

It’s why I enjoy this forum over others… Your members don’t usually write out a long bit of code for someone and then say here you go. People step people through things and allow them to learn the actual process of why the script works so they actually learn the language instead of people doing it for them.

I agree, I love it when people provide the code and where they think the issue is. It allows me and others to pin-point with they are trying to accomplish and provide that mini solution they need to get past a sticking point.

I’m a beginner php coder, but I’m a seasoned developer. I’m really good with Microsoft .Net, IIS, HTML, CSS, and just about every database. I wish I knew more, so I can help more. A lot of times I have to do what people ask in .net, then convert it to PHP.

But somethings just don’t translate…

@valandor062: I took a glance at that tutorial. In essence, it does what I was thinking of doing, using a separate template file for when I need to have a repeat usage. When I checked to see if this was what was done with phpBB, I was surprised to find it wasn’t. This is what has led to my question here. I have a feeling it has to do with their comment labels in their templates, because they don’t appear when you look at the code and yet they’re the only indicator of what piece of the template loops. I’ve just not been able to find reference to it in the files.

Thankfully, I have the basics for template engines right now, which I consider important. The more I can learn how to do certain functionality myself, the better off I can create something that does precisely what I need without extra bloat.

@Topcoder: It had crossed my mind to ask them, though I’m worried if I do that they’ll do what valandor described and just “give me” the code without any sort of means of showing me how to work it out myself. That’s the key here: I want to figure out how to do it. I’m just at a loss for where to start looking.

Sponsor our Newsletter | Privacy Policy | Terms of Service