Click here to Skip to main content
15,888,239 members
Articles / Programming Languages / PHP

Building a Really Simple PHP Templating Engine

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
18 Feb 2014CPOL 10.7K   4   1
How to build a really simple PHP templating engine

Recently, I had to make use of templates in PHP, and as a lot of people on StackOverflow ([1], [2]) suggested, “you don’t need another templating engine like Smarty on top of PHP, as PHP itself is a templating engine”.

So, I ended up making a simple function which looks like this:

PHP
function getTemplate($file, $variables = NULL){
    if (! is_null($variables))
    extract($variables);

    include(TEMPLATES_FOLDER . $file);	
}

TEMPLATES_FOLDER is a PHP constant which is defined in the config.php file like this:

PHP
if ( !defined('ABSPATH') )
	define('ABSPATH', dirname(__FILE__) . '/');

define("TEMPLATES_FOLDER", ABSPATH . "templates/");

So, for example, a template may look like this:

PHP
<!-- Navigation START -->
<div class="navigation">
	<div class="welcome">Hi, <?=$user;?></div>
	<div class="nav">
		<ul>
			<li><a href="home.php" 
                 class="<? echo (isset($currentHome) ? 'current' : '') ?>">Home</a></li>
	        <li><a href="members.php" 
                 class="<? echo (isset($currentMembers) ? 'current' : '')  ?>">Members</a></li>
		</ul>
	</div>

	<div class="clear"></div>
</div>
<!-- Navigation START -->

Here you can see the usage of ternary if operator:

PHP
echo (isset($currentHome) ? 'current' : '')

And a way to call this template from a home.php file would be:

PHP
<? getTemplate("navigation.php", array("user" => getUser(), "currentHome" => true) ); ?>

So the getTemplate() function loads the navigation.php file and passes it its variables “user” and “currentHome” which are then in the getTemplate() function extracted by using the extract() function and echoed out. The currentHome variable is set when called from the home.php file so that a template “knows” to set the “current” class to that element.

Templating engines have their pros and cons, and since I didn’t need much leverage on the templating, this proved to serve me just fine.

What do you use for your template management in PHP?

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Croatia Croatia
I’m an engineer at heart and a jack of all trades kind of guy.

For those who care about titles, I hold a masters degree in computing from FER (and a black belt in karate, but that’s another story…).

During the last years, worked in a betting software industry where I made use of my knowledge in areas ranging from full-stack (web & desktop) development to game development through Linux and database administration and use of various languages (C#, PHP, JavaScript to name just a few).

Currently, I’m a senior software engineer at TelTech, where we make innovative communications apps, and I <3 it.

Lately, I’m very passionate about Ionic framework and am currently in the top 3 answerers on StackOverflow in Ionic framework. I wrote a book about Ionic framework which you can get for free on Leanpub: Ionic framework – step by step from idea through prototyping to the app stores.

Other technical writing:

+ wrote a book Getting MEAN with MEMEs
was a technical reviewer for a book Deploying Node published by Packt
was a technical reviewer for a book Getting started with Ionic published by Packt
After writing 300 posts, this is why I think you should start blogging too

Come and see what I write about on my blog.

Comments and Discussions

 
Questionproject Pin
phpsystems13-Nov-15 21:37
phpsystems13-Nov-15 21:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.