\documentclass{article}
\usepackage{ulem}
\usepackage{graphicx}
\usepackage{hyperref}
\pagestyle{headings}
\begin{document}
\part{Horde\_View}
Documentation on the Horde\_View package. This documentation is adopted from the MAD documentation (<a href="http://framework.maintainable.com/mvc/5\_view.php\">http://framework.maintainable.com/mvc/5\textbackslash\{\}\_view.php\textbackslash\{\}</a>), as many of the Horde View ideas are also adopted from MAD, and the helpers are directly compatible between the two systems.

The following explanations correspond to Horde 5.

\section{Example usage}
\textbf{Calling code}

<pre><code class="language-php">
<?php

require\_once \_\_DIR\_\_ . '/lib/Application.php';
Horde\_Registry::appInit('yourapp');

// use a model to get the data for book authors and titles.
\$data = array(
    array(
        'author' => 'Hernando de Soto',
        'title' => 'The Mystery of Capitalism'
    ),
    array(
        'author' => 'Henry Hazlitt',
        'title' => 'Economics in One Lesson'
    ),
    array(
        'author' => 'Milton Friedman',
        'title' => 'Free to Choose'
    )
);

\$view = new Horde\_View (array('templatePath' => 'viewtest'));
\$view->addHelper('Horde\_View\_Helper\_Tag'); 
\$view->books = \$data;

// and render a template called "template.php"
echo \$view->render('template.php');
</code></pre>
\textbf{View template}

Put the following code into viewtest/template.php (the template path is set above):

<pre><code class="language-php">
<?php if (\$this->books): ?>

<!-- A table of some books. -->
<table>
    <tr>
        <th>Author</th>
        <th>Title</th>
    </tr>

<?php foreach (\$this->books as \$key => \$val): ?>
    <tr>
        <td><?php echo \$this->escape(\$val['author']) ?></td>
        <td><?php echo \$this->escape(\$val['title']) ?></td>
    </tr>
<?php endforeach ?>

</table>

<?php else: ?>

    <p>There are no books to display.</p>

<?php endif ?>
</code></pre>
\section{Base functionality}
\begin{itemize}
\item assign indiv vars


\item assign bulk vars


\item ``<u>isset/</u>empty/\_\_get``


\item add template paths (incl. add missing trailing slash)


\item render (w/ .html added if no extension)


\item render in path order


\item render partials


\item render w/ local variables


\item render partial collections


\item add helpers


\item helper method overwrite


\end{itemize}
\section{Helper functionality}
\subsection{Benchmark}
\begin{itemize}
\item logging


\end{itemize}
\subsection{Capture}
\begin{itemize}
\item capture output (nested output buffers)


\item capture to named view variable


\end{itemize}
\subsection{Debug}
\begin{itemize}
\item pretty variable dump


\end{itemize}
\subsection{Form Tag}
\begin{itemize}
\item generate form tags


\item generate multipart form tags


\item checkbox input


\item hidden input


\item file input


\item password input


\item radio input


\item select input


\item textarea input


\item text field input


\item submit input


\end{itemize}
\subsection{Form}
\begin{itemize}
\item initialize defaults from objects


\item wrapper around FormTag helpers with object awareness


\item formFor(\$object)


\item fieldsFor(\$object)


\item default form builder


\item custom form builders


\end{itemize}
\subsection{Javascript}
\begin{itemize}
\item generate <script> tags


\end{itemize}
\subsection{Number}
\begin{itemize}
\item human-readable file sizes


\end{itemize}
\subsection{Tag}
\begin{itemize}
\item xhtml tags with programmatic options


\item rewrite checked => true to checked="checked"


\item contentTag for tags with nested content


\item generate CDATA sections


\item single-escaping to not re-escape entities


\end{itemize}
\subsection{Text}
\begin{itemize}
\item h() shorthand escape


\item truncate


\item truncate middle


\item highlight


\item cycle


\item pluralize


\end{itemize}
\subsection{Url}
\begin{itemize}
\item linkTo, either from controller-generated links or string URIs


\item default to showing URL as link text


\item linkToUnless


\item linkToIf


\item mailTo


\item mailTo with javascript email address obfuscation


\item mailTo with hex email address obfuscation


\item mailTo with "replace this ..." obfuscation


\end{itemize}
\section{Helpers}
\subsection{Overview}
Views separate the presentation from the controllers and models. Views are allowed to have logic, provided that the logic is only for presentation purposes. This presentation logic is small bits of PHP code embedded in the HTML.

Bits of presentation logic code can be extracted into helper methods. Once extracted, a helper method can be called in the view in place of the former code block. Extracting presentation logic into helpers is a best practice and helps keep views clean and DRY.

Helpers are simply methods of a class. The framework mixes the helpers into the view behind the scenes, and makes them appear as methods inside the view. An example of a helper class with a single \texttt{highlight()} helper follows. For more detailed information, refer to <a href="http://dev.horde.org/api/FRAMEWORK\_4/lib/View/package-View.Helper.html">http://dev.horde.org/api/FRAMEWORK\textbackslash\{\}\_4/lib/View/package-View.Helper.html</a>

\subsection{Example}
In the above example template, a function of Horde\_View\_Helper\_Tag is used. Functions can be overloaded and additional helpers can be defined. For instance, you might whish to highlight a certain name in your booklist. You could define a corresponding function and put it into your own helper class in file yourapp/lib/Helper.php:

<pre><code class="language-php">

<?php
class Yourapp\_Helper extends Horde\_View\_Helper\_Base
\{
    /**
     * Highlights a phrase within the given text.
     *
     * @param string \$text
     * @param string \$phrase
     *
     * @return string
     */
    public function highlight(\$text, \$phrase)
    \{
        if (empty(\$phrase) || empty(\$text)) \{
            return \$text;
        \}
        \$highlighter = '<strong class="highlight">' . \$this->escape(\$phrase) . '</strong>';
        return preg\_replace('/' . preg\_quote(\$phrase) . '/', \$highlighter, \$text); 
    \}
\}
</code></pre>
To use this helper, it helper must be added to the view you are using (w/ref. to above view.php):

<pre><code class="language-php">
\$view->addHelper('Yourapp\_Helper'); 
</code></pre>
And in template.php:

<pre><code class="language-php">
...
       <td><?php echo \$this->highlight(\$val['author'], 'Soto') ?></td>
...
</code></pre>
It is OK to put HTML into helper class methods because they exist to assist with presentation. However, it is NOT OK to put print/echo statements within a helper class. Helper methods always return a value that is displayed in the view like \texttt{<?= \$this->highlight(\$text) ?>}.

\subsection{Organization}
As shown above, helpers are methods that are organized into classes. The framework will mix helper methods together through overloading. Inside a view, helper methods from all of the sources above can be called by simply using \texttt{<?= \$this->helperMethod() ?>}.

\end{document}
