October 15th, 2020 at 10:03 PM
Hello all,
EDIT: See the GitHub version. A new version is available with significant improvements. https://github.com/Darth-Apple/simple-php-templates
I've done quite a few small CMS-style projects for university and for random purposes. Each time, we ended up having to theme the project and it was always easier to do so by separating the HTML from the PHP. Fortunately, there are many template engines available, but some are very tedious to implement and are not suitable for small projects.
Luckily, it's incredibly easy to do templates with no trouble whatsoever. This template engine is incredibly simple and can be added to your project in less than 5 minutes. I use it on every raw project I've ever done, and it's a great time-saver and makes our code much cleaner and easier to maintain.
I've included the template_engine.php file with this post. It's licenced under the GPL 2, but if you need a different license, please PM me!
1. Create a file called template_engine.php with the downloaded file attached here. Include (or require_once) this file into every file/page of your project (such as index.php, etc). You may want to use your require_once("template_engine.php") in your init or global.php instead, depending on how your project is structured.
2. Now, create a templates folder inside your project. This folder should be in your project root and will contain all HTML template files.
3. Instantiate the template engine in each of these pages, or in your init file. Use the following:
4. Start setting template variables/tags in your code. These variables will be replaced with the values they are set to when the template engine outputs your page. For example, in your index.php, you could set variables like this:
Now, the [@some_variable] tag will be replaced by "1234" by the template engine when it's ready to parse. Likewise, [@username] will be replaced with "Steve". Create as many variables as you like.
5. Create your index.html template in your templates folder. Use something like the following:
As you can see, we've used our new template variables directly in the template file. These variables will be replaced by their corresponding values on the fly by the template engine.
6. Parse the template and echo it. This will process all template tags and will send the resulting page to the browser.
And now, with a simple template engine and 5 minutes of implementation, you can abstract away all HTML from your PHP code. This allows you to completely refactor the style and hTML structure of your theme/page without changing any of your PHP files, and keeps the project much cleaner than it would be with interleaved HTML/PHP.
Simple template engines such as these are perfect for small projects, university/college assignments, etc.
Hope this helps! Feel free to use it in your own projects as needed.
More Information (click spoiler):
[spoiler]
Can I change a variable/tag after it's already been set?
Yes! So long as you haven't parsed your template (sent it to the browser) yet, you can use the $templates->set function as many times as you need. It will overwrite the previous value of the tag with your new value.
How should I handle template headers/footers?
I recommend having a separate header.html and footer.html template. At the very beginning of your PHP page, parse these using the following:
Now, in your index.html template, you may use the header/footer tags as normal:
This allows you to use the same header/footer template for all of your pages, preventing you from having to duplicate the HTML in every page of your website.
Can I use templates inside of templates?
Yes! In fact this is recommend and is standard practice for simple template engines such as these. See the example above on how to do this.
What about the license?
Feel free to use this in your own projects, that's what it's for! I wouldn't have licensed this at all, but I don't want someone stealing it, saying they wrote it, and then licensing it under some restrictive license. If you need a different license, please PM me!
[/spoiler]
EDIT: See the GitHub version. A new version is available with significant improvements. https://github.com/Darth-Apple/simple-php-templates
I've done quite a few small CMS-style projects for university and for random purposes. Each time, we ended up having to theme the project and it was always easier to do so by separating the HTML from the PHP. Fortunately, there are many template engines available, but some are very tedious to implement and are not suitable for small projects.
Luckily, it's incredibly easy to do templates with no trouble whatsoever. This template engine is incredibly simple and can be added to your project in less than 5 minutes. I use it on every raw project I've ever done, and it's a great time-saver and makes our code much cleaner and easier to maintain.
I've included the template_engine.php file with this post. It's licenced under the GPL 2, but if you need a different license, please PM me!
1. Create a file called template_engine.php with the downloaded file attached here. Include (or require_once) this file into every file/page of your project (such as index.php, etc). You may want to use your require_once("template_engine.php") in your init or global.php instead, depending on how your project is structured.
2. Now, create a templates folder inside your project. This folder should be in your project root and will contain all HTML template files.
3. Instantiate the template engine in each of these pages, or in your init file. Use the following:
Code:
$templates = new template_engine();
4. Start setting template variables/tags in your code. These variables will be replaced with the values they are set to when the template engine outputs your page. For example, in your index.php, you could set variables like this:
Code:
$templates->set("some_variable", "1234");
$templates->set("username", "Steve");
Now, the [@some_variable] tag will be replaced by "1234" by the template engine when it's ready to parse. Likewise, [@username] will be replaced with "Steve". Create as many variables as you like.
5. Create your index.html template in your templates folder. Use something like the following:
Code:
<head>
<title>My Index Page </title>
</head>
<body>
<h1> Hello, [@username]. Today's magic variable: [@some_variable] </h1>
</body>
As you can see, we've used our new template variables directly in the template file. These variables will be replaced by their corresponding values on the fly by the template engine.
6. Parse the template and echo it. This will process all template tags and will send the resulting page to the browser.
Code:
echo $template->parse("index");
And now, with a simple template engine and 5 minutes of implementation, you can abstract away all HTML from your PHP code. This allows you to completely refactor the style and hTML structure of your theme/page without changing any of your PHP files, and keeps the project much cleaner than it would be with interleaved HTML/PHP.
Simple template engines such as these are perfect for small projects, university/college assignments, etc.
Hope this helps! Feel free to use it in your own projects as needed.
More Information (click spoiler):
[spoiler]
Can I change a variable/tag after it's already been set?
Yes! So long as you haven't parsed your template (sent it to the browser) yet, you can use the $templates->set function as many times as you need. It will overwrite the previous value of the tag with your new value.
How should I handle template headers/footers?
I recommend having a separate header.html and footer.html template. At the very beginning of your PHP page, parse these using the following:
Code:
$header = $templates->parse("header");
$templates->set("header", $header);
$footer = $templates->parse("footer");
$templates->set("footer", $footer);
Now, in your index.html template, you may use the header/footer tags as normal:
Code:
[@header]
<h1> My page content </h1>
[@footer]
This allows you to use the same header/footer template for all of your pages, preventing you from having to duplicate the HTML in every page of your website.
Can I use templates inside of templates?
Yes! In fact this is recommend and is standard practice for simple template engines such as these. See the example above on how to do this.
What about the license?
Feel free to use this in your own projects, that's what it's for! I wouldn't have licensed this at all, but I don't want someone stealing it, saying they wrote it, and then licensing it under some restrictive license. If you need a different license, please PM me!
[/spoiler]