I have tried a number of ready made templates before
writing this. But, none of them seem to me suitable
for me. They were either too complicated, or too simple.
So I decided to write "a template"..
2 main features for ATemplate are:
* Allows nested templates,
* Easy to use.. (I hope so..)
Files:
atemplate.inc.php : ATemplate class
atemplate_test.php : Test file implementing a sample page
template.css : styles used by template.php
tpl/*.tpl : template files for atemplate_test.php
ATemplate class has 5 public functions including constructor:
* atemplate( array tplfiles)
Constructor. Reads required template files into memory. (see. atemplate_test.php)
Order in array is important. Because, It's printed in the same
order.
e.g.
tpl = new atemplate ( array (
MAIN => 'main.tpl',
ROWS => 'rows.tpl',
BOTTOM => 'botton.tpl'
)
);
You can find the template files for this code at the end of this
file..
* process(string tplname, array vars)
Replaces template variables with given ones and prapares
for output! You must call this function to prapare output
even if the template has no variables. If you need multiple
copies for this template, like rows in a table, you can
make multiple calls.
e.g.
$tpl->process(BOTTOM, array()); // notice no parameters!
$tpl->process(ROWS, array(
CITY => 'Ankara',
COUNTRY => 'Turkey' ));
// to be able to insert multiple copies of a template
// continue to process as needed
$tpl->process(ROWS, array(
CITY => 'Paris',
COUNTRY => 'France' ));
$tpl->process(ROWS, array(
CITY => 'Rome',
COUNTRY => 'Italy' ));
$tpl->process(MAIN, array(
TITLE => 'Test page'));
* embed(string whichone, towhich)
Embeds one template's output to another. E.g. You'll have prapared a
general layout like main.tpl, and you'll embed a table with rows
from rows.tpl. You must process the embedded/embedding template
before embed.
e.g.
// now let's embed the rows parapared above, into MAIN:
$tpl->embed(ROWS, MAIN);
// all the output prapared in rows are embedded into MAIN.
// - {ROWS} is replaced by ROWS in MAIN -
// embed BOTTOM, too.
$tpl->embed(BOTTOM, MAIN);
* append(string whichone, towhich)
Sometimes you may need to add one template to another.
See template_test.php for sample usage.
* printit(void)
Writes out generated output.
$tpl->printit();
<!--main.tpl-->
<html>
<head>
<title>{TITLE}</title>
</head>
<body>
<table>
<!-- row.tpl will be inserted here -->
{ROWS}
</table>
{BOTTOM}
</body>
</html>
<!--rows.tpl -->
<tr>
<td>{CITY}</td>
<td>{COUNTRY}</td>
</tr>
<!--bottom.tpl -->
<hr>
<center>Send comments to <a href=mailto:soysal@programmer.net>here</a></center>
|