[ Index ]

PHP Cross Reference of Akelos Framework

title

Body

[close]

/AkActionView/helpers/ -> tag_helper.php (source)

   1  <?php
   2  /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
   3  
   4  // +----------------------------------------------------------------------+
   5  // | Akelos Framework - http://www.akelos.org                             |
   6  // +----------------------------------------------------------------------+
   7  // | Copyright (c) 2002-2006, Akelos Media, S.L.  & Bermi Ferrer Martinez |
   8  // | Released under the GNU Lesser General Public License, see LICENSE.txt|
   9  // +----------------------------------------------------------------------+
  10  
  11  /**
  12   * @package ActionView
  13   * @subpackage Helpers
  14   * @author Bermi Ferrer <bermi a.t akelos c.om>
  15   * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
  16   * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
  17   */
  18  
  19  
  20  /**
  21  * Use these methods to generate HTML tags programmatically when you can't use a Builder. 
  22  * By default, they output XHTML compliant tags.
  23  */
  24  class TagHelper extends AkObject 
  25  {
  26      /**
  27      * Returns an empty HTML tag of type *name* which by default is XHTML 
  28      * compliant. Setting *open* to true will create an open tag compatible 
  29      * with HTML 4.0 and below. Add HTML attributes by passing an attributes 
  30      * array to *options*. For attributes with no value like (disabled and 
  31      * readonly), give it a value of true in the *options* array.
  32      *
  33      * Examples:
  34      * 
  35      *   <%= tag 'br' %>
  36      *    # => <br />
  37      *   <%= tag 'br', null, true %>
  38      *    # => <br>
  39      *   <%= tag 'input', { :type => 'text', :disabled => true } %>
  40      *    # => <input type="text" disabled="disabled" />
  41      */
  42      function tag($name, $options = null, $open = false)
  43      {
  44          return '<'.$name.(!empty($options) ? TagHelper::_tag_options($options) : '').($open ? '>' : ' />');
  45      }
  46  
  47      /**
  48      *  Returns an HTML block tag of type *name* surrounding the *content*. Add
  49      * HTML attributes by passing an attributes array to *options*. For attributes 
  50      * with no value like (disabled and readonly), give it a value of true in 
  51      * the *options* array. You can use symbols or strings for the attribute names.
  52      *
  53      *   <%= content_tag 'p', 'Hello world!' %>
  54      *    # => <p>Hello world!</p>
  55      *   <%= content_tag('div', content_tag('p', "Hello world!"), :class => "strong") %>
  56      *    # => <div class="strong"><p>Hello world!</p></div>
  57      *   <%= content_tag("select", options, :multiple => true) %>
  58      *    # => <select multiple="multiple">...options...</select>
  59      */
  60      function content_tag($name, $content, $options = null)
  61      {
  62          return '<'.$name.(!empty($options) ? TagHelper::_tag_options($options) : '').'>'.$content.'</'.$name.'>';
  63      }
  64  
  65      /**
  66      * Returns a CDATA section for the given +content+.  CDATA sections
  67      * are used to escape blocks of text containing characters which would
  68      * otherwise be recognized as markup. CDATA sections begin with the string
  69      * <tt>&lt;![CDATA[</tt> and } with (and may not contain) the string 
  70      * <tt>]]></tt>. 
  71      */
  72      function cdata_section($content)
  73      {
  74          return '<![CDATA['.$content.']]>';
  75      }
  76  
  77  
  78      /**
  79      * Returns the escaped +html+ without affecting existing escaped entities.
  80      *
  81      *  <%= escape_once "1 > 2 &amp; 3" %>
  82      *    # => "1 &gt; 2 &amp; 3"
  83      */
  84      function escape_once($html)
  85      {
  86          static $charset;
  87          if(empty($charset)){
  88              $charset = Ak::locale('charset');
  89          }
  90          return TagHelper::_fix_double_escape(htmlentities($html, ENT_COMPAT, $charset));
  91      }
  92  
  93      /**
  94      * Fix double-escaped entities, such as &amp;amp;, &amp;#123;, etc.
  95      */
  96      function _fix_double_escape($escaped)
  97      {
  98          return preg_replace('/&amp;([a-z]+|(#\d+));/i', '&$1;', $escaped);
  99      }
 100  
 101      function _tag_options($options)
 102      {
 103          $formated_options = array();
 104          foreach ($options as $key=>$value){
 105              if(empty($value) && !is_string($value)){
 106                  continue;
 107              }
 108              if(!is_numeric($key) && !is_array($value) && !is_object($value)){
 109                  $formated_options[$key] =  $key.'="'.TagHelper::escape_once($value).'"';
 110              }
 111          }
 112          ksort($formated_options);
 113          return empty($formated_options) ? '' : ' '.join(' ',$formated_options);
 114      }
 115  }
 116  
 117  ?>


Generated: Mon Oct 27 12:43:49 2008 Cross-referenced by PHPXref 0.6