\documentclass{article}
\usepackage{ulem}
\usepackage{graphicx}
\usepackage{hyperref}
\pagestyle{headings}
\begin{document}
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:

<pre><code class="language-php">
   require "Horde/String.php";
   require "Horde/Util.php";
   require "Horde/Template.php";
</code></pre>
(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:

<pre><code class="language-php">
  \$template = new Horde\_Template();
</code></pre>
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:

<pre><code class="language-php">
echo \$template->fetch(\$template\_file);
</code></pre>
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.

\subsection{Simple Variables}
PHP:

<pre><code class="language-php">
//In the php you have a variable like:
    \$foo = 'bar';
//Set it into your template using:
    \$template->set('foo', \$foo);
</code></pre>
Template:

<pre><code>
<html><body>
  we have a foo value of <tag:foo />
</body></html>
</code></pre>
** 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. Unparsed tags  are left in the output in their original form, which shouldn't cause any visual distortion of the end result, as it's parsed by browsers as a HTML tag, but ignored because the browser doesn't know what it means.

\subsection{Arrays}
PHP:

<pre><code class="language-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);
</code></pre>
Template:

<pre><code>
<html><body>
  we have many foo values:
  <loop:foo>
    <li><tag:foo />
  </loop:foo>
</body></html>
</code></pre>
** 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.

\subsection{Arrays With Keys}
PHP:

<pre><code class="language-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);
</code></pre>
Template:

<pre><code>
<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>
</code></pre>
** 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.

\subsection{If Conditions}
PHP:

<pre><code class="language-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());
</code></pre>
Template:

<pre><code>
<html><body>
  welcome to our site...<br />
  <if:is\_auth>
    today's site statistics are: <tag:visitors /> visitors!
  </if:is\_auth>
</body></html>
</code></pre>
** 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.

\subsection{If-Else Conditions}
PHP:

<pre><code class="language-php">
    // 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);
</code></pre>
Template:

<pre><code>
<html><body>
  welcome to our site...<br />
  <if:somename>
    somename is true

  <else:somename>
    somename is false
  </else:somename>

  </if:somename>
</body></html>
</code></pre>
** Note:** the <else> statement must be enclosed in the <if> block.

\subsection{If Conditions and Arrays}
PHP:

<pre><code class="language-php">
    // 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);
</code></pre>
Template:

<pre><code>
<html><body>
  welcome to our site...<br />
  <if:users>
    Current users:<br/>

    <loop:users>
        <tag:users /><br/>
    </loop:users>

  <else:users>
    There are no users at the moment
  </else:users>

  </if:users>
</body></html>
</code></pre>
** Note:** the <else> statement must be enclosed in the <if> block.

\subsection{Nested Loops and Nested Tags}
PHP:

<pre><code class="language-php">
\$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]);
\}
</code></pre>
Template:

<pre><code>
<table>
<loop:categories>
<tr>
  <td>
    I have a <tag:categories />. What could it be?
  </td><td>
    <ul>
    <loop:subcat\_<tag:categories />>
      <li><tag:subcat\_<tag:categories /> /></li>
    </loop:subcat\_<tag:categories />>
    </ul>
  </td>
</tr>
</loop:categories>
</table>
</code></pre>
Output:<br />
I have a fruit. What could it be?

\begin{itemize}
\item apple


\item pear


\end{itemize}
I have a veggie. What could it be?

\begin{itemize}
\item tomato


\item potato


\item carrot


\item onion


\end{itemize}
I have a thing. What could it be?

\begin{itemize}
\item spoon


\item paperbag


\item tool


\end{itemize}
\subsection{Backward Compatibility}
It is possible to make Horde\_Template backward compatible to some extent with other engines, eg. class.FastTemplate.php by modifying the functions getTag and getTags with different tag patterns.

This is left as an exercise for the reader.

\end{document}
