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

Source for file Flexy.php

Documentation is available at Flexy.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 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:  Alan Knowles <alan@akkbhome.com>                           |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Flexy.php 334846 2014-09-12 04:50:56Z alan_k $
  20. //
  21. //  Handler code for the <flexy: namespace
  22. //
  23.  
  24. /**
  25. * the <flexy:XXXX namespace
  26. * 
  27. * 
  28. * at present it handles
  29. *       <flexy:toJavascript flexy:prefix="Javascript_prefix"  javscriptName="PHPvar" .....>
  30. *       <flexy:include src="xxx.htm">
  31. *
  32. *
  33. *
  34. * @version    $Id: Flexy.php 334846 2014-09-12 04:50:56Z alan_k $
  35. */
  36.  
  37. class HTML_Template_Flexy_Compiler_Standard_Flexy  {
  38.  
  39.         
  40.     /**
  41.     * Parent Compiler for
  42.     *
  43.     * @var  object  HTML_Template_Flexy_Compiler 
  44.     * 
  45.     * @access public
  46.     */
  47.     var $compiler;
  48.  
  49.    
  50.     /**
  51.     * The current element to parse..
  52.     *
  53.     * @var object 
  54.     * @access public
  55.     */    
  56.     var $element;
  57.     
  58.     
  59.     
  60.     
  61.     
  62.     /**
  63.     * toString - display tag, attributes, postfix and any code in attributes.
  64.     * Relays into namspace::method to get results..
  65.     *
  66.     * 
  67.     * @see parent::toString()
  68.     */
  69.     function toString($element) 
  70.     {
  71.         
  72.         list($namespace,$method) = explode(':',$element->oTag);
  73.         if (!strlen($method)) {
  74.             return '';
  75.         }
  76.         // things we dont handle...
  77.         if (!method_exists($this,$method.'ToString')) {
  78.             return '';
  79.         }
  80.         return $this->{$method.'ToString'}($element);
  81.         
  82.     }
  83.    /**
  84.     * toJavascript handler
  85.     * <flexy:toJavascript flexy:prefix="some_prefix_"  javascriptval="php.val" ....>
  86.     * 
  87.     * @see parent::toString()
  88.     */
  89.     
  90.     function toJavascriptToString($element) 
  91.     {
  92.         $ret = $this->compiler->appendPhp( "require_once 'HTML/Javascript/Convert.php';");
  93.         $ret .= $this->compiler->appendHTML("\n<script type='text/javascript'>\n");
  94.         $prefix = ''. $element->getAttribute('FLEXY:PREFIX');
  95.         
  96.         
  97.         foreach ($element->attributes as $k=>$v) {
  98.             // skip directives..
  99.             if (strpos($k,':')) {
  100.                 continue;
  101.             }
  102.             if ($k == '/') {
  103.                 continue;
  104.             }
  105.             $v = substr($v,1,-1);
  106.             $ret .= $this->compiler->appendPhp(
  107.                 '$__tmp = HTML_Javascript_Convert::convertVar('.$element->toVar($v) .',\''.$prefix . $k.'\',true);'.
  108.                 'echo (is_object($__tmp) && is_a($__tmp,"PEAR_Error")) ? ("<pre>".print_r($__tmp,true)."</pre>") : $__tmp;');
  109.             $ret .= $this->compiler->appendHTML("\n");
  110.         }
  111.         $ret .= $this->compiler->appendHTML("</script>");
  112.         return $ret;
  113.     }
  114.     /**
  115.     * include handler
  116.     * <flexy:include src="test.html">
  117.     * 
  118.     * @see parent::toString()
  119.     */
  120.     function includeToString($element) 
  121.     {
  122.         // this is disabled by default...
  123.         // we ignore modifier pre/suffix
  124.     
  125.     
  126.     
  127.        
  128.         $arg = $element->getAttribute('SRC');
  129.         if (!$arg) {
  130.             return $this->compiler->appendHTML("<B>Flexy:Include without a src=filename</B>");
  131.         }
  132.         // ideally it would be nice to embed the results of one template into another.
  133.         // however that would involve some complex test which would have to stat
  134.         // the child templates anyway..
  135.         // compile the child template....
  136.         // output... include $this->options['compiled_templates'] . $arg . $this->options['locale'] . '.php'
  137.         return $this->compiler->appendPHP( "\n".
  138.                 "\$x = new HTML_Template_Flexy(\$this->options);\n".
  139.                 "\$x->compile('{$arg}');\n".
  140.                 "\$x->outputObject(\$t);\n"
  141.             );
  142.     
  143.     }
  144.     
  145.     /**
  146.     * Convert flexy tokens to HTML_Template_Flexy_Elements.
  147.     *
  148.     * @param    object token to convert into a element.
  149.     * @return   object HTML_Template_Flexy_Element 
  150.     * @access   public
  151.     */
  152.     function toElement($element) 
  153.     {
  154.        return '';
  155.     }
  156.         
  157.     
  158.     /**
  159.     * Handler for User defined functions in templates..
  160.     * <flexy:function name="xxxxx">.... </flexy:block>  // equivilant to function xxxxx() {
  161.     * <flexy:function call="{xxxxx}">.... </flexy:block>  // equivilant to function {$xxxxx}() {
  162.     * <flexy:function call="xxxxx">.... </flexy:block>  // equivilant to function {$xxxxx}() {
  163.     * 
  164.     * This will not handle nested blocks initially!! (and may cause even more problems with
  165.     * if /foreach stuff..!!
  166.     *
  167.     * @param    object token to convert into a element.
  168.     * @access   public
  169.     */
  170.   
  171.     
  172.     function functionToString($element) 
  173.     {
  174.         
  175.         if ($arg = $element->getAttribute('NAME')) {
  176.             // this is a really kludgy way of doing this!!!
  177.             // hopefully the new Template Package will have a sweeter method..
  178.             $GLOBALS['_HTML_TEMPLATE_FLEXY']['prefixOutput']  .= 
  179.                 $this->compiler->appendPHP( 
  180.                     "\nfunction _html_template_flexy_compiler_standard_flexy_{$arg}(\$t,\$this) {\n").
  181.                 $element->compileChildren($this->compiler) .
  182.                 $this->compiler->appendPHP( "\n}\n");
  183.                 
  184.                 return '';
  185.         }
  186.         if (!isset($element->ucAttributes['CALL'])) {
  187.             
  188.             return HTML_Template_Flexy::staticRaiseError(
  189.                 ' tag flexy:function needs an argument call or name'.
  190.                 " Error on Line {$element->line} &lt;{$element->tag}&gt;",
  191.                          null,   HTML_TEMPLATE_FLEXY_ERROR_DIE);
  192.         }
  193.         // call is a  stirng : nice and simple..
  194.         if (is_string($element->ucAttributes['CALL'])) {
  195.             $arg = $element->getAttribute('CALL');
  196.             return $this->compiler->appendPHP( 
  197.                     "if (function_exists('_html_template_flexy_compiler_standard_flexy_'.{$arg})) " .
  198.                     " _html_template_flexy_compiler_standard_flexy_{$arg}(\$t,\$this);");
  199.         }
  200.         
  201.         // we make a big assumption here.. - it should really be error checked..
  202.         // that the {xxx} element is item 1 in the list... 
  203.         $e=$element->ucAttributes['CALL'][1];
  204.         $add = $e->toVar($e->value);
  205.         if (is_object($add) && is_a($add,'PEAR_Error')) {
  206.             return $add;
  207.         } 
  208.         return $this->compiler->appendPHP(
  209.             "if (function_exists('_html_template_flexy_compiler_standard_flexy_'.{$add})) ".
  210.             "call_user_func_array('_html_template_flexy_compiler_standard_flexy_'.{$add},array(\$t,\$this));");
  211.         
  212.         
  213.         
  214.     }
  215.  

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