Posts Tagged ‘Template’

Page class

Tuesday, November 7th, 2006

Simple class to represent a web page with template support, the class is so simple that if you are a coder you can easily read it (sorry for missing comments).

If you are not interested with the implementation I’ll write here a simple explanation of how it works.

Obviously you have to include the file into your code with something like:

require("page_class.php");

After you can instance an object in a really simple manner:

$page = new Page("include/template.tpl");

As you can see the only parameter of constructor is the path of a template page; some methods (and constructor also) can return an exception if something goes wrong, you have to use try and catch.
Probably you want to insert some dynamically generated content into the page, so let’s make the assumption that your template contains some HTML code and some placeholders.
The first thing to do is to select left and right placeholder’s marker:

$page->setPlaceholder("<_?", "?_>");

Now my placeholders have general shape like that: <_?NAME?_>.
If you want to insert some content for replacing a placeholder:

$page->setContent("isazi", "NAME");

The page now has every occurrence of <_?NAME?_> replaced with string isazi.
Maybe in the future i can insert a third parameter if you want to replace only a selected number of placeholders, but this is another story…
Only two methods remain that are useful for everyone, and are:

$page->getPage();

If you want to get the whole page and save it into a string and:

$page->show();

If you want to print the page.

If you want the source, you can download it.