[commits] [Wiki] changed: Doc/Dev/Horde_View

Jan Schneider jan at horde.org
Wed Feb 17 13:44:01 UTC 2016


jan  Wed, 17 Feb 2016 13:44:01 +0000

Modified page: http://wiki.horde.org/Doc/Dev/Horde_View
New Revision:  8
Change log:  Tweaks

@@ -17,25 +17,23 @@
  Horde_Registry::appInit('yourapp');

  // use a model to get the data for book authors and titles.
  $data = array(
-              array(
-                    'author' => 'Hernando de Soto',
+    array(
+        'author' => 'Hernando de Soto',
          'title' => 'The Mystery of Capitalism'
-                    ),
-              array(
-                    'author' => 'Henry Hazlitt',
+    ),
+    array(
+        'author' => 'Henry Hazlitt',
          'title' => 'Economics in One Lesson'
-                    ),
-              array(
-                    'author' => 'Milton Friedman',
+    ),
+    array(
+        '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"
@@ -57,27 +55,28 @@
      </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; ?>
+<?php endforeach ?>

  </table>

  <?php else: ?>
+
      <p>There are no books to display.</p>
-<?php endif; ?>

+<?php endif ?>
  </code>


  ++ Base functionality

  * assign indiv vars
  * assign bulk vars
-* __isset/__empty/__get
+* \__isset/\__empty/\__get
  * add template paths (incl. add missing trailing slash)
  * render (w/ .html added if no extension)
  * render in path order
  * render partials
@@ -117,9 +116,9 @@

  +++ Form

  * initialize defaults from objects
-* wrapper around FormTag helpers with object awareness
+* wrapper around !FormTag helpers with object awareness
  * formFor($object)
  * fieldsFor($object)
  * default form builder
  * custom form builders
@@ -141,8 +140,9 @@
  * single-escaping to not re-escape entities

  +++ Text

+* h() shorthand escape
  * truncate
  * truncate middle
  * highlight
  * cycle
@@ -179,36 +179,36 @@
  <?php
  class Yourapp_Helper extends Horde_View_Helper_Base
  {
      /**
-     * Highlight a phrase within the given text
-     * @param   string  $text
-     * @param   string  $phrase
-     * @return  string
+     * Highlights a phrase within the given text.
+     *
+     * @param string $text
+     * @param string $phrase
+     *
+     * @return string
       */
      public function highlight($text, $phrase)
      {
-        $escaped = $this->escape($phrase);
-        $highlighter = '<strong class="highlight">' . $phrase . '</strong>';
-
          if (empty($phrase) || empty($text)) {
              return $text;
          }
-        return preg_replace("/($phrase)/", $highlighter, $text);
+        $highlighter = '<strong class="highlight">' .  
$this->escape($phrase) . '</strong>';
+        return preg_replace('/' . preg_quote($phrase) . '/',  
$highlighter, $text);
      }
  }
  </code>


  To use this helper, it helper must be added to the view you are  
using (w/ref. to above view.php):
-<code>
+<code type="php">
  $view->addHelper('Yourapp_Helper');
  </code>

  And in template.php:
-<code>
+<code type="php">
  ...
-   		<td><?php echo $this->highlight($val['author'], 'Soto') ?></td>
+       <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