Simple class to represent a web page with template support.
It’s a strange time, many things to do and very few time, so i want to share a simple class that i’ve made last summer (and cleaned in that days), mainly for two reasons:
1) better (for me) comprehension of object oriented programming;
2) one week of work with php for university (they pay me also, cool).
Someone could say “why you don’t write documetation?”, i’ve no excuse, i’m really a slacker.
Ok, let’s start with something more serious (this is NOT a blog).
Class is really simple, if you are a coder you can easily read it (sorry for missing comments, but remember… i’m a slacker), if you are a coder but you’re not interested with the implementation i’ll write here a simple explanation.
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.








0 responses so far ↓
There are no comments yet...Kick things off by filling out the form below.
Leave a Comment