Jump to content

Template Engine Blocks


Skye McCloud

Recommended Posts

I've been trying to create a simple, custom template engine in order for me to understand the basics of how they work, and to use on small scale websites I'm working on. While I have the basics down, there's one function, which to me seems like it should be incredibly simple, that I can't figure out: blocks. Blocks are suppose to allow one to reuse a portion of a template file repeatedly (this is often done with PHP-based forums these days, to show a list of forums, a list of topics, or even a set of posts within a topic), with labels indicating when the block begins and ends.

 

However, I'm at a loss for how to even begin with this. I've tried to find tutorials on how to do this, but all I ever find are tutorials related to pre-existing template engines like Smarty. I have confidence I can modify any solutions to work with how I've programmed my template engine, so what I'm trying to find is a starting point for code.

Link to comment
Share on other sites

The reason I don't show is because, outside of my basic template engine (which works fine and I have no problems with), I have nothing to show. I'm at that much of a loss on working with blocks that I can't even figure out where to start. I've been trying to work through the logic of it, spending way too much time analyzing an existing template engine that already does what I'm looking for (the one used in phpBB) in an effort to figure it out.

 

However, like most existing engines, it has a deeper level of complication that could take me weeks, if not months, to finally sort out. However, I don't have weeks to sort through it all enough to build a guess idea of how it works (In addition, if I had that, I'd have a lot more confidence in my own ability to fix it).

 

As such, what I'm after is where I start and what I could look at to get me started.

Link to comment
Share on other sites

I guess the best advice I would give is build a simple 3-4 page website and see where the code duplication exists.  From there you will see where OOP really comes into play.   "Anywhere" you see the same code repeated, make a function.  "Anywhere" you see multiple functions repeat themselves, make a class.

 

PHP manual is your friend.

http://php.net/manual/en/language.oop5.php

 

what does your index.php look like right now?

Link to comment
Share on other sites

 


However, like most existing engines, it has a deeper level of complication that could take me weeks, if not months, to finally sort out.

 

Actually, the problem is quite simple if you think of these "blocks" as separate templates.  If you can render a template then it's allmost a trivial extension to find and parse a subtemplate. The only difference is that a regular template has access to the entire scope of data, and the subtemplate has to use elements of an array etc. But even that comes down to allmost exactly what it says: loop through the array and parse the subtemplate with the elementdata.

 

 

 


However, I don't have weeks to sort through it all enough to build a guess idea of how it works (In addition, if I had that, I'd have a lot more confidence in my own ability to fix it).

 

If you have a deadline then use an existing template system. The reason why systems like Smarty are so large is that you need those functions.

Link to comment
Share on other sites

You need to lex your templates as seen here:

https://github.com/fabpot/Twig/blob/master/lib/Twig/Lexer.php

 

Once your template is lexed, you have a token stream (to see how that might look like see token_get_all), this token stream can then be used to validate your templates as well as to start parsing your template.

 

A good book about lexing/parsing see http://www.amazon.com/Language-Implementation-Patterns-Domain-Specific-Programming/dp/193435645X

Edited by ignace
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.