Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Building a really simple PHP templating engine

, 18 Feb 2014
Rate this:
Please Sign up or sign in to vote.
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: [

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:

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:

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

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

So, for example a template may look like this:

<!-- 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:

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

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

<? 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)

Share

About the Author

Nikola Breznjak
Web Developer
Croatia Croatia
There is no short term solution for a long term result.
 
My blog: http://www.nikola-breznjak.com/blog/
Follow on   Twitter   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 18 Feb 2014
Article Copyright 2014 by Nikola Breznjak
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid