[dev] Horde templates short examples
Marko
marko at oblo.com
Thu Dec 19 16:21:56 PST 2002
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.
having set up the tamplate object:
$template = &new Horde_Template();
you can proceed with feeding variables into the object.
SIMPLE VARIABLES
----
PHP:
//In the php you have a variable like:
$foo = 'bar';
//Set it into your template using:
$template->set('foo', $foo);
TEMPLATE:
<html><body>
we have a foo value of <tag:foo />
</body></html>
** Note: the <tag:... /> has to be exactly like above, with one space between
the foo and />. Any other variation and the value will not be parsed.
ARRAYS
----
PHP:
//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:
<html><body>
we have many foo values:
<loop:foo>
<li><tag:foo />
</loop:foo>
</body></html>
** Note: use an 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 get parsed.
ARRAY 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:
<html><body>
we have many foo values:
<loop:foo>
<li><tag:foo.city />, located in <tag:foo.country /> where <tag:foo.language
/> is spoken
</loop:foo>
</body></html>
** Note: you need to have a loop array to parse through variables with keys.
using the tags <tag:foo.somekey /> on their own without the loop tag will not work.
IF CONDITIONS
----
PHP:
//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:
<html><body>
welcome to our site...<br />
<if:is_auth>
today's site statistics are: <tag:visitors /> visitors!
</if:is_auth>
</body></html>
** Note: as always the syntax has to be precise, and the part within the
<if:..></if:..> block will be shown only if $is_auth in the php sets the tag
value of is_auth to true.
i'll try and post a more complex template once i clean them up a bit.
More information about the dev
mailing list