Easy templating in PHP
Even though the subject of “Using templates in php” is everywhere, it still didn’t reach it’s true potential.
Most people who are new to php(not sure about more advanced php developers) use methods like this to put templates in their website:
1 2 3 | <?php include 'header.txt'; // A simple include ?> |
As you can see it’s not very sophisticated, and you can use it only with normal HTML files.
People who are more advanced use this:
1 2 3 | <?php include 'header.php'; ?> |
It’s almost the same as the first one, only that you can also put in there PHP code.
The problem with all of those is that as more parts in your template you have the more files your’e going to need.
So if you have three parts like:
head
menu
footer
Then your’e going to need three files:
head.php
menu.php
footer.php
And the more parts the more the files to maintain, So if you’ll need to change one of those file’s name on a 100 paged website your’e doomed.
So what I came up with about one year after I learned php is using one file for all your templating needs.
The idea is to have a main php file for your template(I call mine “template.php”) which you include once in every page you use your template.
An example of a template file:
1 2 3 4 5 6 7 8 | <?php function head(){ // Some content for header } function foot(){ // Some content for footer } ?> |
Simple as that, you just declare a function for each part of your template, so for instance if you want to add a menu part to your template you add a function called “menu”, like this:
1 2 3 4 5 6 7 8 9 10 11 | <?php function head(){ // Some content for header } function menu(){ // Some content for header } function foot(){ // Some content for footer } ?> |
But how do I use it inside a page?
To use it inside a page where you want to use this templating system you include the “template.php” file somewhere in the page before you use the template’s functions, and then just use the functions where you want your template’s parts to show.
An example of page using this templating system:
1 2 3 4 5 6 7 8 9 | <?php include 'template.php'; head(); // use this where you want the content you've putten inside the "head" function ?> Some content of your page <?php foot(); // use this where you want the content you've putten inside the "foot" function ?> |
You don’t have to stop there
You can also add variables for your functions, like add a “title” variable for the head function.
And example of a template file with variables used:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?php function head($title=''){ // add ='' for pages which do not have a title ?> <html> <head> <title><?=$title?></title> </head> <body> <?php } function foot(){ ?> </body> </html> <?php } ?> |
There are a lot more ways to use this templating system, but I’ll leave it up to you to make it like you want.
Note: If you have a lot of parts in your template it’s better to use several files like I showed you.
This just feels wrong.
What do you mean?
I guess he means that it is way easier to maintain code in one particular file out of 100 than to search and maintain a function inside one large single file. Oh, he’s right btw.
If you want you can divide your template into several sub-files by subject or however you want.
You can use it if you want, but I find it a lot simpler than handling 100’s of files, and most people don’t need a lot parts so using one file can help them.