HTML_Template_Flexy
[ class tree: HTML_Template_Flexy ] [ index: HTML_Template_Flexy ] [ all elements ]

Source for file CustomFlexyAttributes.php

Documentation is available at CustomFlexyAttributes.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2002 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors:  Rick < reick at coalescentdesign dot com >                 |
  17. // +----------------------------------------------------------------------+
  18. //
  19. /** Class called from HTML_Template_Flexy_Compiler_Flexy_Tag's toString() method.
  20. *    For handling new custom flexy namespaced attributes.
  21. */
  22. class HTML_Template_Flexy_Compiler_Flexy_CustomFlexyAttributes  
  23. {
  24.  
  25.     /**
  26.     * doCustomAttributes
  27.     * - for every flexy namespaced attribute found in the element parameter,
  28.     *    if there is a method here to handle it then call the method.
  29.     *
  30.     * @params   object HTML_Template_Flexy_Token
  31.     * @return   none 
  32.     * @access   public
  33.     */
  34.     function doCustomAttributes(&$element
  35.     {
  36.         
  37.         foreach ($element->ucAttributes as $key=>$value{
  38.             
  39.             list($namespace,$attribute(strpos($key":"> -1explode(':',$key: array(""$key);
  40.             $method strtolower($attribute'Attribute';
  41.             if (strtolower($namespace!= 'flexy'{
  42.                 return;
  43.             }
  44.             if ((strlen($attribute)) && (method_exists($this,$method))) {
  45.                 $this->{$method}($element$element->getAttribute($key));
  46.             }
  47.         }  
  48.     }
  49.  
  50.    /**
  51.     * flexy:content attribute handler
  52.     *
  53.     * Examples:
  54.     * <anyTag... flexy:content="methodOrVariableOrNothing" .../>
  55.     * <anyTag... flexy:content="methodOrVariableOrNothing" ...></anyTag>
  56.     * <anyTag... flexy:content="methodOrVariableOrNothing" ...>All this <b>CONTENT</b> will be <i>replaced<i> by
  57.     * the result of methodOrVariableOrNothing</anyTag>
  58.     *
  59.     * Replaces element content with the result of the variable or method call or empty string.
  60.     * Useful for replacing example content/html from the compiled template, while keeping it in the source
  61.     * template for viewing when not running php. The example content/html will be replaced by dynamic content at run-time.
  62.     *
  63.     * Substitute for <anyTag...>{methodOrVariable}</anyTag>
  64.     * 
  65.     * @params   object HTML_Template_Flexy_Token
  66.     * @params   string attribute value
  67.     * @return   none 
  68.     * @access   private
  69.     */
  70.     function contentAttribute(&$element,$val
  71.     {
  72.         // assign method or variable $val as the child token of this element, potentially replacing any existing children
  73.         // default: special case if $val is empty - simply set children to null
  74.         $element->children = null;
  75.         if (empty($val)) {
  76.             $this->replaceChildren($element,$val);
  77.         }  
  78.  
  79.         // do we have to add a seperate closing tag token to surround the content within...
  80.         if ($element->close)  {
  81.             return;
  82.         }
  83.         if ($element->getAttribute('/'!== false)  {
  84.                 // valid xhtml (eg. <tag />)
  85.                 // remove the '/' since we must add a seperate closing tag token to surround the content within
  86.             unset($element->attributes['/']);
  87.             unset($element->ucAttributes['/']);
  88.         }  else {
  89.             // FIXME: error not valid xhtml 
  90.         }
  91.         
  92.         // add a seperate closing tag token to surround the content within
  93.         $element->close = $element->factory("EndTag",array('/'.$element->oTag)$element->line);
  94.     
  95.     
  96.     /**
  97.     * flexy:replace attribute handler
  98.     *
  99.     * Examples:
  100.     * <anyTag... flexy:replace="methodOrVariableOrNothing" .../>
  101.     * <anyTag... flexy:replace="methodOrVariableOrNothing" ...></anyTag>
  102.     * <anyTag... flexy:replace="methodOrVariableOrNothing" ...>Entire <b>element</b> including tag <i>replaced<i> by
  103.     * the result of methodOrVariableOrNothing</anyTag>
  104.     *
  105.     * Replaces entire element with the result of the variable or method call or empty string.
  106.     * Useful for removing example html from the compiled template, while keeping it in the source template for viewing
  107.     * when not running php. The example html will be replaced by dynamic content at run-time.
  108.     *
  109.     * Substitute for {methodOrVariable}
  110.     * 
  111.     * @params   object HTML_Template_Flexy_Token
  112.     * @params   string attribute value
  113.     * @return   none 
  114.     * @access   private
  115.     */
  116.     function replaceAttribute(&$element,$val
  117.     {
  118.         // Setting tag to empty will prevent the opening and closing tags from beinging displayed
  119.         $element->tag = null;
  120.         $element->oTag = null;
  121.  
  122.         // assign method or variable $val as the child token of this element, potentially replacing any existing children
  123.         // special case if $val is empty - simply set children to null
  124.         $element->children = null;
  125.         if (empty($val)) {
  126.             //echo '<br/>VAL IS: ' . $val;
  127.             $this->replaceChildren($element,$val);
  128.         }    
  129.     }
  130.  
  131.     /**
  132.     * flexy:omittag attribute handler
  133.     * <... flexy:omittag ...>
  134.     * <... flexy:omittag="" ...>
  135.     * <... flexy:omittag="anything" ...>
  136.     * Removes the tag but keeps the contents of the element including child elements. This is
  137.     * useful for flexy:if and flexy:foreach when the tag isn't wanted but you would
  138.     * prefer not to use {} placeholders for conditionals and loops.
  139.     * 
  140.     * @params   object HTML_Template_Flexy_Token
  141.     * @params   string attribute value
  142.     * @return   none 
  143.     * @access   private
  144.     */
  145.     function omittagAttribute(&$element,$val
  146.     {
  147.         // Setting tag to empty will prevent the opening and closing tags from beinging displayed
  148.         $element->tag = null;
  149.         $element->oTag = null;
  150.     }
  151.  
  152.     /**
  153.     * replaceChildren
  154.     * - replaces element children with the method or variable Token generated from the $val parameter
  155.     * 
  156.     * @params   object HTML_Template_Flexy_Token
  157.     * @params   string attribute value
  158.     * @return   none 
  159.     * @access   private
  160.     */
  161.     function replaceChildren(&$element,&$val)
  162.     {
  163.         // Most of the this method is borrowed from parseAttributeIf() in HTML_Template_Flexy_Compiler_Flexy_Tag
  164.         
  165.         // If this is a method, not a variable (last character is ')' )...
  166.         if (substr($val,-1== ')'{
  167.             // grab args..
  168.             $args substr($val,strpos($val,'(')+1,-1);
  169.             // simple explode ...
  170.             
  171.             $args strlen(trim($args)) explode(',',$args: array();
  172.             //print_R($args);
  173.             
  174.             // this is nasty... - we need to check for quotes = eg. # at beg. & end..
  175.             $args_clean = array();
  176.             // clean all method arguments...
  177.             for ($i=0; $i<count($args)$i++{
  178.                 if ($args[$i]{0!= '#'{
  179.                     $args_clean[$args[$i];
  180.                     continue;
  181.                 }
  182.                 // single # - so , must be inside..
  183.                 if ((strlen($args[$i]> 1&& ($args[$i]{strlen($args[$i])-1}=='#')) {
  184.                     $args_clean[$args[$i];
  185.                     continue;
  186.                 }
  187.                 
  188.                 $args[$i.=',' $args[$i+1];
  189.                 // remove args+1..
  190.                 array_splice($args,$i+1,1);
  191.                 $i--;
  192.                 // reparse..
  193.             }
  194.             
  195.             //echo('<br/>VAL: ' . $val . ' is seen as method');
  196.             
  197.             $childToken =  $element->factory('Method',array(substr($val,0,strpos($val,'('))$args_clean)$element->line);
  198.         else {
  199.  
  200.             //echo('<br/>VAL: ' . $val . ' is seen as var');
  201.             $childToken =  $element->factory('Var''{'.$val.'}'$element->line);
  202.         }
  203.  
  204.         $element->children = array($childToken);
  205.         
  206.     }
  207. }

Documentation generated on Mon, 11 Mar 2019 15:15:24 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.