Jump to content

Template Engines - Repeat use of a portion of a template file


Skye McCloud

Recommended Posts

Recently I've been learning how to create a template engine, partially for my own knowledge and partially because I find most pre-existing template engines, like Smarty, carry a lot more functions in them than I need.

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 using 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).

You just create a looping construct in your template. How you do this depends on how you are doing your templates. If you do like smarty for instance and use {tag} placeholders then you'd create a tag such as {foreach}. As you parse the template you extract the text between {foreach} and {/foreach} and use that in a loop.

 

This is just off the top of my head and untested, so there may be bugs, but as a basic example:

$tplVars['var']=array(1,2,3,4);
$template = 'Before the loop.  {foreach $var}In the loop!{/foreach} After the loop!';
for ($pos=0; ($pos=strpos($template,'{foreach ',$pos)) !== false; $pos++){
   $end = strpos($template,$pos+1);
   if ($end === false) die('Error');

   $end += 10; //length of '{/foreach}'
   //Extract the code for the loop from the template (including tags)
   $code = substr($template, $pos, $end-$pos);

   //Extract the variable name
   $varToLoop = substr($code, 0, strpos($code, '}'));
   $varToLoop = substr($varToLoop, strrpos($varToLoop, ' '));

   //Strip the tags from the code
   $code = substr($code, strpos($code, '}')+1);
   $code = substr($code, 0, strrpos($code, '{'));

   if (!isset($tplVars[$varToLoop])) die('Error');

   $output = '';
   foreach ($tplVars[$varToLoop] as $var){
      $output .= $code;
   }

   //Replace the tags in the original template w/ the generated output.
   $template = substr_replace($template, $output, $pos, $end-$pos);
}
var_dump($template);
On the other hand, you could just let PHP handle the templating for you in which case you don't need to do anything other than write a normal loop. This is what I do:

Before the loop!
<?php foreach ($var as $v): ?>
   In the loop!
<?php endforeach; ?>
After the loop!
When you want to use the template, it's just a simple matter of include()ing it.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.