|
-
Jun 14th, 2008, 04:57 PM
#1
-
Jun 14th, 2008, 06:34 PM
#2
Hyperactive Member
Re: Question about templates
They're not including PHP code in them, they're parsing the template file (probably with regular expressions) to be able to run the conditionals in PHP and perform the appropriate action.
I have a simple template parser that supports simple blocks and phrases.. Code and example below:
PHP Code:
/**
* Parse the style's template (Replace the variables)
*s
* @param string templateName
*
* @returns string Parsed Template Contents
**/
private function parseTemplate($templateName) {
// Read file
$templateContents = file_get_contents(ROOT . "styles/" . $this->StyleData['FolderName'] . "/" . $this->Templates[$templateName]['FileName']);
// String template replacements
preg_match_all('/\$magebb\{(.*?)\}/i', $templateContents, $varMatches);
$replaceme = array();
$replacements = array();
foreach($varMatches[1] As $key => $matchName) {
if(isset($this->Variables[$matchName]))
{ $replaceme[$key] = $varMatches[0][$key]; $replacements[$key] = $this->Variables[$matchName]; }
else
{ $replaceme[$key] = $varMatches[0][$key]; $replacements[$key] = "Style error: ".$varMatches[0][$key]." is undefined."; }
}
$templateContents = str_replace($replaceme, $replacements, $templateContents);
preg_match_all('/\$openBlock\{(.*?)\}(.*)\$closeBlock\{(.*?)\}/i', $templateContents, $blockMatches);
if(count($blockMatches[0]) < 1) return $templateContents;
foreach($blockMatches[1] As $key => $matchName) {
if(isset($this->Blocks[$matchName])) {
$replacementHTML = "";
preg_match_all('/\$blockItem\{(.*?)\}/i', $blockMatches[2][$key], $itemMatches);
foreach($this->Blocks[$matchName] As $block) {
$replaceme = array();
$replacements = array();
foreach($itemMatches[1] As $itemKey => $itemMatchName) {
if(isset($block[$itemMatchName]))
{ $replaceme[$itemKey] = $itemMatches[0][$itemKey]; $replacements[$itemKey] = $block[$itemMatchName]; }
else
{ $replaceme[$itemKey] = $itemMatches[0][$itemKey]; $replacements[$itemKey] = "Style error: ".$itemMatches[0][$itemKey]." is undefined in block ".$matchName."."; }
}
$replacementHTML .= str_replace($replaceme, $replacements, $blockMatches[2][$key]);
}
$templateContents = str_replace($blockMatches[0], $replacementHTML, $templateContents);
} else { $templateContents = str_replace($blockMatches[0][$key], "Style error: Block '".$matchName."' is undefined.", $templateContents); }
}
//echo '<pre>';
//print_r($blockMatches);
return $templateContents;
}
Blocks can be setup like this:
PHP Code:
$this->Blocks['menuitems'][0]['link'] = "index.php";
$this->Blocks['menuitems'][0]['title'] = "Home";
$this->Blocks['menuitems'][1]['link'] = "index.php";
$this->Blocks['menuitems'][1]['title'] = "Test";
Example:
Code:
<div id="menu">
$openBlock{menuitems}
<div class="menuitem">
<a href="$blockItem{link}">$blockItem{title}</a>
</div><div class="menusep"></div>
$closeBlock{menuitems}
<div style="clear: both"></div>
</div>
» Twitter: @rudi_visser : Website: www.rudiv.se «
If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.
-
Jun 14th, 2008, 07:05 PM
#3
Re: Question about templates
Oh ok, thanks Rudi. I had never heard of a templating system but your code helps a lot and I am also reading further into it (like Smarty, etc.).
Thanks.
-
Jun 14th, 2008, 07:13 PM
#4
Hyperactive Member
Re: [RESOLVED] Question about templates
You're welcome.
I've never used Smarty myself but it looks very powerful. Template systems always seemed daunting and boring to me but I knew that for my forum software I'd need one that worked and was very simple, and I wanted to write my own system too.
That's when I set about writing this, it's not the best, but it does the job. I'm soon going to be writing in some conditional stuff, when I do I'll post the code here, if you're still looking for something.
» Twitter: @rudi_visser : Website: www.rudiv.se «
If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.
-
Jun 15th, 2008, 11:35 AM
#5
Re: [RESOLVED] Question about templates
Smarty seems really good but also kind of bloated for some of the smaller stuff I'll be doing. I like how simple yours is. Conditional statements would be nice, though, I hope you plan on adding them in soon ( ). Otherwise, you need to call the conditional statements from the (encoded) PHP script and have separate .tpl files for each condition (unless I am mistaken).
Edit: Sorry, forgot to rate your post.
-
Jun 15th, 2008, 11:54 AM
#6
Hyperactive Member
Re: [RESOLVED] Question about templates
Well it could be made easier depending on what type of conditions you want. I mean, do you want full if(whatever==whatever) { do whatever } or would you be happy with something like:
HTML Code:
$condition{logged_in}
you are logged in
$else{logged_in}
you are not logged in
$endcondition{logged_in}
Which you could setup like:
PHP Code:
$this->Conditions['logged_in'] = (empty($username)?false:true);
I'm sure you'll get the idea of what I mean and why it would be easier.
» Twitter: @rudi_visser : Website: www.rudiv.se «
If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.
-
Jun 18th, 2008, 11:23 PM
#7
Re: [RESOLVED] Question about templates
No need for full conditionals, even just the simple ones you have in there would be fine.
-
Jun 18th, 2008, 11:38 PM
#8
Re: [RESOLVED] Question about templates
For simple templates, I just include them as PHP code.
A more in-depth possible way would be to make an XML schema for your templates and use XSL to transform them into parseable PHP.
Even more in-depth would be to treat each template as a 'black box' from the point of view of the application. Each template would be given a set of inputs and produces a certain output for each possible input. The input/output pairs would then be cached somehow, making it possible to bypass the template engine altogether while running the application.
There are many methods.
Last edited by penagate; Jun 18th, 2008 at 11:41 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|