[commits] [Wiki] changed: Doc/Dev/Horde_View
Wiki Guest
wikiguest at horde.org
Wed Jan 21 09:14:22 UTC 2015
guest [178.26.86.13] Wed, 21 Jan 2015 09:14:22 +0000
Modified page: http://wiki.horde.org/Doc/Dev/Horde_View
New Revision: 7
Change log: Updated to Horde 5, fixed code bugs, added some description
@@ -2,17 +2,20 @@
+ 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.
+
+The following explanations correspond to Horde 5.
++ Example usage
**Calling code**
<code type="php">
<?php
-require 'Horde/Autoloader.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(
@@ -27,17 +30,22 @@
'author' => 'Milton Friedman',
'title' => 'Free to Choose'
)
);
+
+$view = new Horde_View (array('templatePath' => 'viewtest'));
$view = new Horde_View;
+$view->addHelper('Horde_View_Helper_Tag');
$view->books = $data;
// and render a template called "template.php"
echo $view->render('template.php');
</code>
**View template**
+
+Put the following code into viewtest/template.php (the template path
is set above):
<code type="php">
<?php if ($this->books): ?>
@@ -49,10 +57,10 @@
</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>
+ <td><?php echo $this->escape($val['author']) ?></td>
+ <td><?php echo $this->escape($val['title']) ?></td>
</tr>
<?php endforeach; ?>
</table>
@@ -61,8 +69,9 @@
<p>There are no books to display.</p>
<?php endif; ?>
</code>
+
++ Base functionality
* assign indiv vars
@@ -132,9 +141,8 @@
* single-escaping to not re-escape entities
+++ Text
-* h() shorthand escape
* truncate
* truncate middle
* highlight
* cycle
@@ -159,12 +167,18 @@
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
{{highlight()}} helper follows:
+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
{{highlight()}} helper follows. For more detailed information, refer
to
http://dev.horde.org/api/FRAMEWORK_4/lib/View/package-View.Helper.html
+
++++ 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:
<code type="php">
-class UsersHelper extends ApplicationHelper
+
+<?php
+class Yourapp_Helper extends Horde_View_Helper_Base
{
/**
* Highlight a phrase within the given text
* @param string $text
@@ -172,22 +186,29 @@
* @return string
*/
public function highlight($text, $phrase)
{
- $escaped = $this->h($text);
- $highlighter = '<strong class="highlight">$escaped</strong>';
-
+ $escaped = $this->escape($phrase);
+ $highlighter = '<strong class="highlight">' . $phrase . '</strong>';
+
if (empty($phrase) || empty($text)) {
return $text;
}
- return preg_replace("/($phrase)/", $highlighter, $text);
+ return preg_replace("/($phrase)/", $highlighter, $text);
}
}
</code>
-And in the HTML template:
+
+To use this helper, it helper must be added to the view you are using
(w/ref. to above view.php):
+<code>
+$view->addHelper('Yourapp_Helper');
+</code>
+
+And in template.php:
<code>
-<div><?= $this->highlight($this->var, 'bob') ?><div>
+...
+ <td><?php echo $this->highlight($val['author'], 'Soto') ?></td>
...
</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) ?>}}.
More information about the commits
mailing list