0

I'm working on a template system that first and foremost needs to be efficient and secondly needs to be easily readable.

I have done something nearly identical before, but it involved preg_match_all - hopelessly inefficient.

So, say we have the HTML like this:

<body>
     <h1><!--[%variable]--></h1>
     <b><!--[%array['key']]--></b>
     <div>
         <!--[template:templateName]-->
     </div>
</body>

So as you can see, we have a variable, an array key and embedded template, that should be replaced with (literally) with : $variable , $array['key'] and parseTemplate($templateName).

Its confusing to explain.

I need access to most variables, which seems to a problem, especially as the template can have templates embedded in it. I'm at a total loss at the moment. Though as usual when coding, I'm massively sleep deprived.

As I need the syntax to be efficient, the above HTML syntax is unnecessary, I currently have in my template file:

<?php
$template[$templateName] = '<body>
     <h1>'.$variable.'</h1>
     <b>'.$array['key'].'</b>
     <div>
         '.parseTemplate('templateName', $varsToPass).'
     </div>
 </body>
 ?>

Where parseTemplate() =

function parseTemplate($tpl, $vars){
    global $rootDir;

    foreach($vars as $var){
        global $$var;
    }

    require_once($rootDir.'/templates/'.$tpl.'.tpl.php');
    return $template[$tpl];
}

The problem with that is, you have to pass $vars down through each multiple level, and if multiple pages are using the same template, the vars you want to pass down through multiple levels.

So if anyone has any ideas on how to do this let me know, I appreciate it. I've formulated a few ideas just by typing it out, but I look forward to hearing others ideas.

Thank you

Josh Dredge
  • 107
  • 1
  • 1
  • 10
  • are you just looking to integrate some smarty-like functionality within your app? or are you actually writing your own template engine? the reason I ask is that Smarty can be integrated and you can use it just as a template engine without using their typical .TPL file template system. Another idea you could look at http://phpsavant.com/ which is an all-PHP template engine – Jason Apr 30 '13 at 03:50
  • Hi, I'm writing my own "template engine". I managed to figure out what I want, I was just looking at it all the wrong way. Thanks for your advice – Josh Dredge Apr 30 '13 at 05:17

0 Answers0