[RESOLVED] Question about templates
Not sure how to put this...
Let's say you are developing a commercial script. To prevent someone from copying/reselling your code, you encode your PHP scripts with something like Zend Encoder.
But now, whoever buys your scripts are unable to change anything design-wise except through external CSS or something.
So, what is a good way to deploy scripts but still giving the customer control over the design/HTML?
One script I've seen (from GoldCoders [yuck]), uses these files called something.tpl which just has the HTML and then some inline code which looks like PHP code but it is embedded with { } braces. :confused: :confused:
If you want to see what I mean, you can download the free script and look at the code:
http://www.goldcoders.com/?page=hyiplister
Here is a snippet...
HTML Code:
<td colspan=4>
{if $listing.hyip_status == 1}<img src="images/m_pay.gif" border=0 alt="Paying" title="Paying" align=absmiddle>{/if}
{if $listing.hyip_status == 2}<img src="images/m_wait.gif" border=0 alt="Waiting" title="Waiting" align=absmiddle>{/if}
{if $listing.hyip_status == 3}<img src="images/m_prob.gif" border=0 alt="Problem" title="Problem" align=absmiddle>{/if}
{if $listing.hyip_status == 4}<img src="images/m_npay.gif" border=0 alt="Not Paying" title="Not Paying" align=absmiddle>{/if}
</td>
I'm guessing those .tpl files are included() but how are they embedding PHP code in them?
What is a good way to deploy encoded PHP scripts, and still letting the customer change the design?
I use external CSS files for all of the design-related content but I think more control would be better...
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>
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.
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.
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. :eek:
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.
Re: [RESOLVED] Question about templates
No need for full conditionals, even just the simple ones you have in there would be fine. :cool:
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.