PHP & Templating
Probably one of the cool features of php for novice developers is a possibility to mix code and design elements in one file. It’s let you start writing your scripts very fast.
However when complexity of your scripts grows it’s become too hard to change design of your pages. Known solve of this problem is a design templates. There are several solutions available out there. I want to share with you my own solution. I’ve developed a very simple class to work with templates. It’s very simple to use and ultra fast at runtime.
Template is a usual html page with special placeholders for dynamic content. They look’s like
%%TITLE%%
%%AUTOR%%
etc.
Download TIvTemplate.
Here is an example of using this class
<?php
include('ivtemplate.php');
$t = new TIvTemplate('MyTemplate.inc');
// Set placeholder values
$t->Set('%%TITLE%%','Iv template system tests');
$t->Set('%%FIRSTLINE%%','First line substitution');
$t->Set('%%SECONDLINE%%','Author of this page is a '.$t->GetConst('AUTHOR'));
// 'render' page
echo $t->FinalResult();
?>
Simple! Isn’t it?
When you got the idea read about Blitz and Smarty systems.
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Leave a Reply