[cvs] [Wiki] created: Doc/Dev/Horde_View

Chuck Hagenbuch chuck at horde.org
Thu Feb 19 22:43:28 UTC 2009


chuck  Thu, 19 Feb 2009 17:43:28 -0500

Created page: http://wiki.horde.org/Doc/Dev/Horde_View

+ Horde_View

Documentation on the Horde_View package. This documentation is adopted  
from the MAD documentation  
(http://framework.maintainable.com/mvc/5_view.php), as many of the  
Horde View ideas are also adopted from MAD, and the helpers are  
directly compatible between the two systems.

++ Helpers

+++ 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 views clean and DRY.

Helpers are simply methods of a class. The framework mixes the helpers  
into the view behind the scenes, and makes the appear as methods  
inside the view. An example of a helper class with a single  
{{highlight()}} helper follows:

<code type="php">
class UsersHelper extends ApplicationHelper
{
     /**
      * Highlight a phrase within the given text
      * @param   string  $text
      * @param   string  $phrase
      * @return  string
      */
     public function highlight($text, $phrase)
     {
         $escaped = $this->h($text);
         $highlighter = '<strong class="highlight">$excaped</strong>';

         if (empty($phrase) || empty($text)) {
             return $text;
         }
         return preg_replace("/($phrase)/", $highlighter, $text);
     }
}
</code>

And in the HTML template:
<code>
<div><?= $this->highlight($this->var, 'bob') ?><div>
...
</code>

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 {{<?= $this->highlight($text) ?>}}.

+++ 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 be called  
by simply using {{<?= $this->helperMethod() ?>}}.



More information about the cvs mailing list