CNET News

Tip of the Moment

In Windows XP/Vista/7 open the 'Start' menu and in the box at the bottom type 'MSCONFIG' and hit [ENTER]. On the fourth tab from the left, across the top titled 'Startup' open that and then select the programs that you need to start with your computer every time it starts (this will not be everything!) then un-check the programs that you don't need to start every time.  Click 'Apply' and then 'Okay' and you my friend have saved some (often considerable) time off your startup!  FYI make sure you DO NOT prevent your anti-virus and firewall from starting up :)

Templater Tutorial PDF Print E-mail
Written by Todd Nestor   
Thursday, 21 July 2011 04:53

So in web development it is the most effective to separate design elements (XHTML, CSS, XML, etc.) from the programming (PHP, JavaScript, Perl, etc.). Typically you use some sort of template to do this, there are a variety of free ones out there (most popular probably being smarty for PHP programming). I use PHP for most of my web applications and I came across a free templater years ago (htmltmpl created by Tomas Styblo http://htmltmpl.sourceforge.net/index.htm) that I use.

 

I took this templater and added into HerculesCore, a framework I have been working on for several years now, originally inspired by a former boss Alma Tuck that I worked for at Core Logic Systems. He had created it, although the version I just released includes almost none of the original framework. So to follow this tutorial you are first going to need to download and install the framework. So if you haven’t done that yet, you’ll need to go follow that tutorial first, its called Installing up and Getting started with HerculesCore, it’s a short tutorial, you should be able to knock it out in 10-15 minutes.

Alright, now let’s begin, open up whatever program you use to edit php/HTML (I use Komodo Edit 5). We’re going to start out the php file and load core, and the class we need is a located in System folder, templater subfolder, so our initial code should look something like this:

<?php

//require prepend file

require('minicore/_conf/conf.php');

//Call for necessary class families from minicore

using ('System.Templater');

Next we need to create instances of the classes we’re going to use, this tutorial is strictly to get you started with the template, so the two classes used for it are the only two we need to initiate:

//Create new instances of all classes used in this page

$manager = new TemplateManager(); //this is the template manager used for preparing the templates that are going to be used

$tproc = new TemplateProcessor(); //this is the template processor which actually takes the prepared template and replaces the variables with their values, also used to set the values

As the comments show TemplateManager is the class that is used to call up template files and get them ready to be used. TemplateProcessor actually takes that prepared template and replaces those variables in the HTML with the values we’ve set them, it also includes the function that sets those values as you’ll see in a moment.

First though we need to prepare our templates, so your next line of code looks like this:

//set templating variables

$template =& $manager->prepare("minicore/template/tutorial.tmp.php"); //this is to be the main template

“minicore/template/tutorial.tmp.php” can be any address of the file you want to load, I typically put them in a folder called template or templates, sometimes I have multiple templates in separate folders in there that I can switch back and forth between, then your code might look like this:

//set templating variables

$template =& $manager->prepare("minicore/template/”.$tmpl_folder.”tutorial.tmp.php"); //this is to be the main template

But for now we’ll stick to just having a template in the template folder. So alright, our template is prepared, now we need to set some variables that can be placed in our html file, these are created with the following function:

$tproc->set(variable_name,”variable value”);

Where variable_name is any name you want to give the variable and “variable value” is whatever you want it to be. Note this does need to be a string of some sort, but it can be the outcome of a function, and it can and often does include some HTML (this may even be a result of a previously compiled template even, but there will be more on that in the next templater tutorial). So I’m going to set a few variables, here’s what the code looks like:

//set template variables ---> the variables you actually put in the HTML file to be replaced by the PHP

$tproc->set(title,"Cool, Inc.");

$tproc->set(someText,"My name is Mattie, I wish I had a Cattie!");

$tproc->set(copy,date('Y'));

$tproc->set(variableWithHTML," <div style=\"font-size: 100px;\">Hi there</div>");

So now that some variables are set we need to process the template and either set it to another variable (next tutorial) or print/echo it (I always use echo, but either way works). We also need to include our append file (if you recall this is optional) for core and close out the PHP page, so here is the rest of the code:

//send the compiled template filled in to the browser

echo $tproc->process($template);

 

//require append file with various data

require('minicore/_conf/append.php');

 

?>

Just in case you need it the following is the code for the entire PHP page I saved as templater_tutorial.php:

<?php

//require prepend file

require('minicore/_conf/conf.php');

//Call for necessary class families from minicore

using ('System.Templater');

//Create new instances of all classes used in this page

$manager = new TemplateManager(); //this is the template manager used for preparing the templates that are going to be used

$tproc = new TemplateProcessor(); //this is the template processor which actually takes the prepared template and replaces the variables with their values, also used to set the values

//set templating variables

$template =& $manager->prepare("minicore/template/tutorial.tmp.php"); //this is to be the main template

//set template variables ---> the variables you actually put in the HTML file to be replaced by the PHP

$tproc->set(title,"Cool, Inc.");

$tproc->set(someText,"My name is Mattie, I wish I had a Cattie!");

$tproc->set(copy,date('Y'));

$tproc->set(variableWithHTML," <div style=\"font-size: 100px;\">Hi there</div>");

//send the compiled template filled in to the browser

echo $tproc->process($template);

//require append file with various data

require('minicore/_conf/append.php');

?>

Now, it may feel like we’re finished, but wait! We still haven’t created the template!

So now let’s start an HTML file and place our variables. Variables are placed by using the

<TMPL_VAR variable ESCAPE=”NONE”>

tag, where variable is any variable we set with the $tproc->set() method. This page should look like any other webpage you’ve created, just place these tags wherever you want PHP to place the value you assigned it, I’m not here to teach you HTML so here is the code I used for the file tutorial.tmp.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">

<html lang="en">

<head>

<title><TMPL_VAR title ESCAPE="NONE"></title>

</head>

<body>

<TMPL_VAR title ESCAPE="NONE">

<br /><br /><br />

<TMPL_VAR someText ESCAPE="NONE">

 

<TMPL_VAR variableWithHTML ESCAPE="NONE">

<br /><br /><br />

<div style="position: relative; top: 50px; width: 500px;">

Copyright <TMPL_VAR title ESCAPE="NONE"> <TMPL_VAR copy ESCAPE="NONE">. All rights reserved.

</div>

</body>

</html>

This is fairly typical, we have a title variable, a copy variable that just returns the year for the the copyright line (I made it simple by using the title for the company name in the copyright line as well). So as you can see you can use the same variable as many times as you want, this page should return the following:

Insert image here

Feel free to email me with any questions: This e-mail address is being protected from spambots. You need JavaScript enabled to view it

Last Updated on Thursday, 21 July 2011 05:07