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.      * reference to the compiler
  26.      * ($compiler->flexy is the base code)
  27.      */
  28.     var $compiler
  29.  
  30.     function __construct($compiler)
  31.     {
  32.         $this->compiler $compiler;
  33.     }
  34.  
  35.     /**
  36.     * doCustomAttributes
  37.     * - for every flexy namespaced attribute found in the element parameter,
  38.     *    if there is a method here to handle it then call the method.
  39.     *
  40.     * @params   object HTML_Template_Flexy_Token
  41.     * @return   none 
  42.     * @access   public
  43.     */
  44.     function doCustomAttributes(&$element
  45.     {
  46.         //print_R($element->ucAttributes);
  47.         foreach ($element->ucAttributes as $key=>$value{
  48.             
  49.             list($namespace,$attribute(strpos($key":"> -1explode(':',$key: array(""$key);
  50.             $method strtolower($attribute'Attribute';
  51.             //echo $method . "\n";
  52.             if (strtolower($namespace!= 'flexy'{
  53.                 continue;
  54.             }
  55.             if ((strlen($attribute)) && (method_exists($this,$method))) {
  56.                 $this->{$method}($element$value);
  57.             }
  58.         }  
  59.     }
  60.  
  61.    /**
  62.     * flexy:content attribute handler
  63.     *
  64.     * Examples:
  65.     * <anyTag... flexy:content="methodOrVariableOrNothing" .../>
  66.     * <anyTag... flexy:content="methodOrVariableOrNothing" ...></anyTag>
  67.     * 
  68.     * <anyTag... flexy:content="methodOrVariableOrNothing" ...>All this <b>CONTENT</b> will be <i>replaced<i> by
  69.     *
  70.     * complex replacements can use the engine here..
  71.     * <anyTag... flexy:content="{xxxx[tttt].methodOrVariableOrNothing()}" ...>All this <b>CONTENT</b> will be <i>replaced<i> by
  72.    
  73.     * 
  74.     * the result of methodOrVariableOrNothing</anyTag>
  75.     *
  76.     * Replaces element content with the result of the variable or method call or empty string.
  77.     * Useful for replacing example content/html from the compiled template, while keeping it in the source
  78.     * template for viewing when not running php. The example content/html will be replaced by dynamic content at run-time.
  79.     *
  80.     * Substitute for <anyTag...>{methodOrVariable}</anyTag>
  81.     * 
  82.     * @params   object HTML_Template_Flexy_Token
  83.     * @params   string attribute value
  84.     * @return   none 
  85.     * @access   private
  86.     */
  87.     function contentAttribute(&$element,$val
  88.     {
  89.         // assign method or variable $val as the child token of this element, potentially replacing any existing children
  90.         // default: special case if $val is empty - simply set children to null
  91.         //$element->children = null;
  92.         //echo "content attribute";
  93.         //var_dump($val);
  94.         $str'';
  95.         foreach($element->children as $c{
  96.             $str .= is_object($c$c->toString('';
  97.         }
  98.         $kval $val;
  99.         if (is_array($val)) {
  100.             $kval '';
  101.             foreach($val as $c{
  102.                 $kval .= is_object($c$c->toString('';
  103.             }
  104.             //var_dump($kval);
  105.         }
  106.         $element->children = null;
  107.         
  108.         if (empty($val)) {
  109.             
  110.         
  111.             $this->compiler->contentStrings[$kval$str;
  112.             $this->replaceChildren($element,$val);
  113.         }
  114.         
  115.         // do we have to add a seperate closing tag token to surround the content within...
  116.         if ($element->close)  {
  117.             return;
  118.         }
  119.         if ($element->getAttribute('/'!== false)  {
  120.                 // valid xhtml (eg. <tag />)
  121.                 // remove the '/' since we must add a seperate closing tag token to surround the content within
  122.             unset($element->attributes['/']);
  123.             unset($element->ucAttributes['/']);
  124.         }  else {
  125.             // FIXME: error not valid xhtml 
  126.         }
  127.         
  128.         // add a seperate closing tag token to surround the content within
  129.         $element->close = $element->factory("EndTag",array('/'.$element->oTag)$element->line);
  130.     
  131.     
  132.     /**
  133.     * flexy:replace attribute handler
  134.     *
  135.     * Examples:
  136.     * <anyTag... flexy:replace="methodOrVariableOrNothing" .../>
  137.     * <anyTag... flexy:replace="methodOrVariableOrNothing" ...></anyTag>
  138.     * <anyTag... flexy:replace="methodOrVariableOrNothing" ...>Entire <b>element</b> including tag <i>replaced<i> by
  139.     * 
  140.     * the result of methodOrVariableOrNothing</anyTag>
  141.     *
  142.     * Replaces entire element with the result of the variable or method call or empty string.
  143.     * Useful for removing example html from the compiled template, while keeping it in the source template for viewing
  144.     * when not running php. The example html will be replaced by dynamic content at run-time.
  145.     *
  146.     * Substitute for {methodOrVariable}
  147.     * 
  148.     * @params   object HTML_Template_Flexy_Token
  149.     * @params   string attribute value
  150.     * @return   none 
  151.     * @access   private
  152.     */
  153.     function replaceAttribute(&$element,$val
  154.     {
  155.         // Setting tag to empty will prevent the opening and closing tags from beinging displayed
  156.         $element->tag = null;
  157.         $element->oTag = null;
  158.  
  159.         // assign method or variable $val as the child token of this element, potentially replacing any existing children
  160.         // special case if $val is empty - simply set children to null
  161.         $element->children = null;
  162.         if (empty($val)) {
  163.             //echo '<br/>VAL IS: ' . $val;
  164.             $this->replaceChildren($element,$val);
  165.         }    
  166.     }
  167.  
  168.     /**
  169.     * flexy:omittag attribute handler
  170.     * <... flexy:omittag ...>
  171.     * <... flexy:omittag="" ...>
  172.     * <... flexy:omittag="anything" ...>
  173.     * Removes the tag but keeps the contents of the element including child elements. This is
  174.     * useful for flexy:if and flexy:foreach when the tag isn't wanted but you would
  175.     * prefer not to use {} placeholders for conditionals and loops.
  176.     * 
  177.     * @params   object HTML_Template_Flexy_Token
  178.     * @params   string attribute value
  179.     * @return   none 
  180.     * @access   private
  181.     */
  182.     function omittagAttribute(&$element,$val
  183.     {
  184.         // Setting tag to empty will prevent the opening and closing tags from beinging displayed
  185.         $element->tag = null;
  186.         $element->oTag = null;
  187.     }
  188.  
  189.     /**
  190.     * replaceChildren
  191.     * - replaces element children with the method or variable Token generated from the $val parameter
  192.     * 
  193.     * @params   object HTML_Template_Flexy_Token
  194.     * @params   string attribute value
  195.     * @return   none 
  196.     * @access   private
  197.     */
  198.     function replaceChildren(&$element,&$val)
  199.     {
  200.         // Most of the this method is borrowed from parseAttributeIf() in HTML_Template_Flexy_Compiler_Flexy_Tag
  201.         if (is_array($val)) {
  202.             $element->children = array($val[1])// should be the token_val
  203.             return;
  204.             
  205.         }
  206.         
  207.         
  208.         
  209.         
  210.         // If this is a method, not a variable (last character is ')' )...
  211.         if (substr($val,-1== ')'{
  212.             // grab args..
  213.             $args substr($val,strpos($val,'(')+1,-1);
  214.             // simple explode ...
  215.             
  216.             $args strlen(trim($args)) explode(',',$args: array();
  217.             //print_R($args);
  218.             
  219.             // this is nasty... - we need to check for quotes = eg. # at beg. & end..
  220.             $args_clean = array();
  221.             // clean all method arguments...
  222.             for ($i=0; $i<count($args)$i++{
  223.                 if ($args[$i]{0!= '#'{
  224.                     $args_clean[$args[$i];
  225.                     continue;
  226.                 }
  227.                 // single # - so , must be inside..
  228.                 if ((strlen($args[$i]> 1&& ($args[$i]{strlen($args[$i])-1}=='#')) {
  229.                     $args_clean[$args[$i];
  230.                     continue;
  231.                 }
  232.                 
  233.                 $args[$i.=',' $args[$i+1];
  234.                 // remove args+1..
  235.                 array_splice($args,$i+1,1);
  236.                 $i--;
  237.                 // reparse..
  238.             }
  239.             
  240.             //echo('<br/>VAL: ' . $val . ' is seen as method');
  241.             
  242.             $childToken =  $element->factory('Method',array(substr($val,0,strpos($val,'('))$args_clean)$element->line);
  243.         else {
  244.  
  245.             //echo('<br/>VAL: ' . $val . ' is seen as var');
  246.             $childToken =  $element->factory('Var''{'.$val.'}'$element->line);
  247.         }
  248.  
  249.         $element->children = array($childToken);
  250.         
  251.         // move flexy:if's End postfix of the start tag to the child token
  252.         if (!$element->close && $element->postfix{
  253.             $element->children = array_merge($element->children$element->postfix);
  254.             $element->postfix = '';
  255.         }
  256.  
  257.  
  258.     }
  259. }

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