These are just some basic examples on using Horde Templates... hopefully so others won't have to go picking the code apart to figure it out, some are obvious, some less so. The absolute minimum to get in the required functions for Horde_Template to work:
require "Horde/String.php";
require "Horde/Util.php";
require "Horde/Template.php";
(You're probably better off installing Horde's base.php or core.php, although it will load the whole Horde core, rather than just the templating engine, hence the above gives less of an overhead)
Having set up the template object:
$template = new Horde_Template();
If you want to fetch templates from a different base directory, then specify an argument to the "new" call with that base directory.
You can proceed with feeding variables into the object.
Once you're done feeding in variables, you need to get your PHP code to output the resulting HTML, which you can do with:
echo $template->fetch($template_file);
NB. If you make the PHP script do any output of its own, outside of the Horde_Template system, it will appear in the output positioned relative to the position of the above fetch() call in the code.
Simple Variables
PHP:
//In the php you have a variable like:
$foo = 'bar';
//Set it into your template using:
$template->set('foo', $foo);
Template:
we have a foo value of
** Note:** the
//our php now builds an array to pass to the templates
$foo = array('apples', 'pears', 'peaches');
//and it is set into the template object
$template->set('foo', $foo);
Template:
we have many foo values:
** Note:** use a loop construct to go through the foo tag and pull out all the values of foo. Again the syntax has to be precise or the tags will not be parsed.
Arrays With Keys
PHP:
//We now have an array with keys to pass
$foo = array('city' => 'paris',
'country' => 'france',
'language' => 'french'
);
//Again it is set the same way into the template object
$template->set('foo', $foo);
Template:
we have many foo values:
, located in where is spoken
** Note:** you need to have a loop array to parse through variables with keys. Using the tags
// Set up the php variable, this checks if a user has been authorised
// the checkUserAuth() function returning a true or false
$is_auth = checkUserAuth();
// Set the 'if' variable into the template object, note the third parameter
// which is being passed this time - it does nothing more than indicate to
// the template parser that this is a variable for which an IF condition
// will be checked. It has nothing to do with the actual value of the variable.
$template->set('is_auth', $is_auth, true);
// And we set up another variable for inclusion inside the if statement
$template->set('visitors', countvisits());
Template:
welcome to our site...
today's site statistics are: visitors!
** Note:** as always the syntax has to be precise, and the part within the
// the first true is the values set to the variable
// the second true indicates it is going to be used in an if statement.
$template->set('somename', true, true);
Template:
welcome to our site...
somename is true
somename is false
** Note:** the
// the first true is the values set to the variable
// the second true indicates it is going to be used in an if statement.
$users = array('john', 'peter', 'mary');
$template->set('users', $users, true);
Template:
welcome to our site...
Current users:
There are no users at the moment
** Note:** the
$categories = array('fruit', 'veggie', 'thing');
$subcats = array('fruit' => array('apple', 'pear'),
'veggie' => array('tomato', 'potato', 'carrot', 'onion'),
'thing' => array('spoon', 'paperbag', 'tool'));
$template->set('categories', $categories);
foreach ($categories as $c) {
$template->set('subcat_' . $c, $subcats[$c]);
}
Template:
I have a . What could it be?
>
/>
>
Output: