PHP custom template tpl loop help

TE = template engine(s)

in the 80’s 90’s we did all kinds of fun things with cars. Example throwing ford ranger suspension under a ford pinto along with maybe a 351w for fun :-). Why? Why not? it was fun, and never got stuck in the snow.

I am that way with programming as well (I have lots of hobbies lol). I am ware of TE on the web, and some are very nice, and will eventually try them, but PHP does it well enough that you can tweak it for your personal needs.

NOTE: I am VERY VERY burned out on the PHP internal templating debate. This is a given, but it makes for messy code, SO no comments on that aspect please.


foreach(array as $row=>$data) {
  if(some arg == some arg){
        do this
} else 
  do some stuff with it for presentation


I am wrote a proprietary template engine for my site. works perfect (even a custom procedural MVC too ). the only part of it that is sloppy is the looping. it is uh- uh- glee!!!

the pattern is the same as most mvc, url = controller. my controller does all the dirty work then sends all the info to the render and then display.

What I want to do is expand on this. I need to get the loops to work in the effect of {-loop $keyword col:5}
loop the data in a table 5 cells wide.

I did some searching on existing TE, and very hard to decipher what they are doing (one reason I am not a fan of OOP - “don’t go there - start a diff topic or email me”)

So any tips or suggestions (will even do a kludge since it is my own system). I was looking into searching string with wild card, but no go. was hoping php had a built in function for that (preg_match??)

My goal is to make this a fully CLEANED up TE for other affiliates that work with the products I do (and there are thousands out there that do, the only problem, is the parent company only offers one pre buit website, so all their affiliates have a basic site that pretty much looks like their competitors - I puts some other basic sites up over the last 10yrs, but I want something more pro. that is geared specifically for these products.

thanks again!!! Love this site - got many good ideas from it. So thought i would join

You’re on the right lines, you’ll need to use preg_match or similar to handle the loops.

Decide what your syntax is going to be then write a regular expression to match it and repeat the matched section however many times.

For example you can use preg_replace_callback

preg_replace_callback('/\{loop ([^\}])\}(.+?)\{\/loop\}/s', function($matches) use ($data) {
   $output = '';
   foreach ($data[$matches[1] as $record) {
        $output .= replace_variables($matches[2], $record);
}, $template);

Assuming $template is the template string, $data is an array of data bound to the template and replace_variables is a function that replaces simple vars like {name}. This would then match


and repeat whatever is in $data['users']. Take a look at preg_replace_callback as it’s the function you’re looking for to achieve this.

Note: This code is untested and intended to point you in the right direction.

I’ll just finish with two points:

  1. This has been done to death since 2001 and there are hundreds or thousands of examples of it online , it’s probably not worth reinventing the wheel unless you have something new to add

Hi tom! thanks for the fast reply
You are correct - it has been done to death, because of that it becomes difficult searching for it. typing in “customs template loops” or something similar, gives all kinds of responses, and for me, doesnt really apply. I agree with the preg match, however, unless you use it frequently, it comes across as very cryptic to make work, thus why avoided it.
the only way I see it, is in the example given, I would have to break down the keyword, the word “loop” so the engine knows Im dealing with some type of loop, then the “col:” for how many columns in a table I want to make, then the value of col - I know smarty does something similar. I might have to rip that apart and see how they did it LOL.
I could make the tables with one key word, but then I run in to a problem if someone want more than 5 or less than 5 columns. Just trying to keep it dynamic. Currently, since it is for me, it is pretty static, but eventually I want to make it public.

thanks for the tips… i’ll see what I can take from it.


PS Love that link! It is SOOOOOOOO true
*gets back on high horse and waves

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.