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