Simple object orientated template system

I’ve always been a fan of the wordpress template system, Its easy to use and very powerful. Expression Engine has a similar template system in place and today we will be writing a class which will host many of the same features and will give you an extremely flexible and powerful template engine to work with.

Structuring our class

class template {
	private $globals = array();

	public function __construct(){}

	public function global_vars( $key, $value = null ) 	{

	}

	public function load( $tpl, $arr = array(), $return = false ) 	{

	}
}

The global_vars function will be used to store variables which we know we will be using in the majority of our templates such as the site title, user name and so on…

Our main function will be load. We will use this function to load a template file, parse the array for the data we want to use in our template and then either return the template in a string OR echo the template there and then.

So to start with we will build the global_var function. This will be a simple recursive function which we can pass either one variable to or an array and it will be stored in $globals and used when we load a template.

public function global_vars( $key, $value = null ) {
	if( is_array( $key ) ) 	{
		foreach( $key as $k => $v ) {
			$this->global_vars( $k, $v );
		}
	} else {
		$this->globals[ $key ] = $value;
	}
}

So what we’ve got going on here is quite straight forward. When we call this function it will first check to see it $key is an array, if it is then it will loop through the array and pass the data to its self. This is called a recursive function, you want to be careful you don’t end up with any never ending loops here!!. If $key is not an array then it will simply store the data in $globals use $key as the array key and $value as the value.

And thats all we need for that function, Now onto load.

First of all with load we are going to check if the template exists.

public function load( $tpl, $arr = array(), $return = false ) {
	if( file_exists( $tpl ) ) {
		/* File exists!! */
	} else {
		return false;
	}
}

If you were using this for an application you were building then you may want to pre-define which folder the template’s are suppose to be held in so to do this you would need to set set a variable as the folder name and then when you are checking to see if the file exists call the variable before the template name.

Now that we know if the template exists or not we need to open the template place it into a string and then either return it or echo it. We also need to loop through our data to prepare it to be used in our template.

public function load( $tpl, $arr = array(), $return = false ) {
	if( file_exists( $tpl ) ) {
		foreach( $arr as $key => $value ) {
			$$key = $value;
		}
		unset( $arr );

		foreach( $this->globals as $key => $value ) {
			$$key = $value;
		}

		ob_start();
		require_once( $tpl );
		$template = ob_get_contents();
		ob_end_clean();

		if( $return == false ) {
			echo $template;
		} else {
			return $template;
		}
	} else {
		return false;
	}
}

Thats all there for our load function. You can see that when we loop through our data we are then setting a variable using the array key as the variable name and the array value as the value. So for example if we had passed an array to the function with an array key of “title” and a value of “Testing title” then in our template we could now echo $title; to get Testing title.

Using our template class

Save your template class as template.class.php on your testing server and here is an example of how you can use this template class.

The template file
Save as template.php

<html>
<html>
<head>
<title><?php echo $site_title; ?></title>
</head>
<body>
<?php echo $hello_world; ?>
</body>
</html>

The index file
Save as index.php

<?php
global_vars( "site_title", "Testing site" );
$data = array(
	"items" => array("item 1","item 2", item 3")
);
$template->load->( "template.php", $data );
?>

We can also use a loop by passing a 3 dimensional array to the script.

<html>
<head>
<title><?php echo $site_title; ?></title>
</head>
<body>
      <?php foreach( $items as $item ) : ?> <?php echo $item; ?>
      <?php endforeach; ?>
</body>
</html>

The PHP code.

<?php
global_vars( "site_title", "Testing site" );

$data = array(
	"items" => array("item 1","item 2", item 3")
);

$template->load->( "template.php", $data );

?>

If we run this script we would see

<br />
item 1<br />
item 2<br />
item 3<br />

I hope you enjoyed and learned something from this tutorial and post below if you have any questions.

Leave a Reply

Your email address will not be published. Required fields are marked *