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. // | Authors:  Tobias dot eberle at gmx dot de (include with vars)        |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: Flexy.php 334846 2014-09-12 04:50:56Z alan_k $
  21. //
  22. //  Handler code for the <flexy: namespace
  23. //
  24.  
  25. /**
  26. * the <flexy:XXXX namespace
  27. * at present it handles
  28. *       <flexy:toJavascript flexy:prefix="Javascript_prefix"  javscriptName="PHPvar" .....>
  29. *       <flexy:include src="xxx.htm">
  30. *
  31. *
  32. *
  33. @version    $Id: Flexy.php 334846 2014-09-12 04:50:56Z alan_k $
  34. */
  35.  
  36. class HTML_Template_Flexy_Compiler_Flexy_Flexy  {
  37.  
  38.         
  39.     /**
  40.     * Parent Compiler for
  41.     *
  42.     * @var  object  HTML_Template_Flexy_Compiler 
  43.     * 
  44.     * @access public
  45.     */
  46.     var $compiler;
  47.  
  48.    
  49.     /**
  50.     * The current element to parse..
  51.     *
  52.     * @var object 
  53.     * @access public
  54.     */    
  55.     var $element;
  56.     
  57.     
  58.     
  59.     
  60.     
  61.     /**
  62.     * toString - display tag, attributes, postfix and any code in attributes.
  63.     * Relays into namspace::method to get results..
  64.     *
  65.     * 
  66.     * @see parent::toString()
  67.     */
  68.     function toString($element
  69.     {
  70.         
  71.         list($namespace,$methodexplode(':',$element->oTag);
  72.         if (!strlen($method)) {
  73.             return '';
  74.         }
  75.         // things we dont handle...
  76.         if (!method_exists($this,$method.'ToString')) {
  77.             return '';
  78.         }
  79.         return $this->{$method.'ToString'}($element);
  80.         
  81.     }
  82.    /**
  83.     * toJavascript handler
  84.     * <flexy:toJavascript flexy:prefix="some_prefix_"  javascriptval="php.val" ....>
  85.     * 
  86.     * @see parent::toString()
  87.     */
  88.     
  89.     function toJavascriptToString($element
  90.     {
  91.         $ret $this->compiler->appendPhp"require_once 'HTML/Javascript/Convert.php';");
  92.         $ret .= $this->compiler->appendHTML("\n<script type='text/javascript'>\n");
  93.         $prefix ''$element->getAttribute('FLEXY:PREFIX');
  94.         
  95.         
  96.         foreach ($element->attributes as $k=>$v{
  97.             // skip directives..
  98.             if (strpos($k,':')) {
  99.                 continue;
  100.             }
  101.             if ($k == '/'{
  102.                 continue;
  103.             }
  104.             $v substr($v,1,-1);
  105.             $ret .= $this->compiler->appendPhp(
  106.                 '$__tmp = HTML_Javascript_Convert::convertVar('.$element->toVar($v.',\''.$prefix $k.'\',true);'.
  107.                 'echo (is_object($__tmp) && is_a($__tmp,"PEAR_Error")) ? ("<pre>".print_r($__tmp,true)."</pre>") : $__tmp;');
  108.             $ret .= $this->compiler->appendHTML("\n");
  109.         }
  110.         $ret .= $this->compiler->appendHTML("</script>");
  111.         return $ret;
  112.     }
  113.     
  114.     /**
  115.     * toJavascript handler
  116.     * <flexy:toJSON  javascriptval="php.val" ....>
  117.     * 
  118.     * @see parent::toString()
  119.     */
  120.     
  121.     function toJSONToString($element
  122.     {
  123.         // maybe should use extnsion_exists....
  124.         $ret "";
  125.         if (!function_exists('json_encode')) {
  126.             $ret $this->compiler->appendPhp
  127.                 'require_once "Services/JSON.php"; $_json = new Services_JSON();'
  128.             );
  129.         
  130.         
  131.         //$ret = $this->compiler->appendPhp( "require_once 'HTML/Javascript/Convert.php';");
  132.         $ret .= $this->compiler->appendHTML("\n<script type='text/javascript'>\n");
  133.         //$prefix = ''. $element->getAttribute('FLEXY:PREFIX');
  134.         
  135.         
  136.         foreach ($element->attributes as $k=>$v{
  137.             // skip directives..
  138.             if (strpos($k,':')) {
  139.                 continue;
  140.             }
  141.             if ($k == '/'{
  142.                 continue;
  143.             }
  144.             $v substr($v,1,-1);
  145.             if (function_exists('json_encode')) {
  146.                 $ret .= $this->compiler->appendPhp(
  147.                     'echo "var '$k .'=" . json_encode('.$element->toVar($v).') . ";\n";'
  148.                 );
  149.                 $ret .= $this->compiler->appendHTML("\n");
  150.                 continue;
  151.             }
  152.             $ret .= $this->compiler->appendPhp(
  153.                     'echo "var '.$k.'=" . $_json->encodeUnsafe('.$element->toVar($v).') . ";\n";'
  154.             );
  155.            
  156.             $ret .= $this->compiler->appendHTML("\n");
  157.         }
  158.         $ret .= $this->compiler->appendHTML("</script>");
  159.         return $ret;
  160.     }
  161.     
  162.     /**
  163.     * include handler
  164.     * <flexy:include src="test.html">
  165.     * <flexy:include src="{test}">
  166.     * <flexy:include src="{test}.html">
  167.     *
  168.     * or include without parsing  (does not support {xxx} args )
  169.     *  <flexy:include src="test.html" type="raw">
  170.     *
  171.     * or to include a file as a js value = used by javascript templates
  172.     *  <flexy:include src="test.html" type="json" name="my_js_variable">
  173.     *
  174.     * @see parent::toString()
  175.     */
  176.     function includeToString($element
  177.     {
  178.         // this is disabled by default...
  179.         // we ignore modifier pre/suffix
  180.     
  181.      
  182.         
  183.         if (!isset($element->ucAttributes['SRC'])) {
  184.             return $this->compiler->appendHTML("<B>Flexy:Include without a src=filename (Line: {$element->line})</B>");
  185.         }
  186.         $arg = $element->ucAttributes['SRC'];
  187.         
  188.         
  189.         // support type = raw.
  190.         if (!empty($element->ucAttributes['TYPE']&& strtolower(trim($element->getAttribute('TYPE'))) == 'raw'{
  191.             
  192.             $arg = "'"$element->getAttribute('SRC')."'";
  193.             return $this->compiler->appendPHP"\n".
  194.                 "\$x = new HTML_Template_Flexy(\$this->options);\n".
  195.                 " include \$x->resolvePath({$arg});\n"
  196.             );
  197.         }
  198.         
  199.         if (!empty($element->ucAttributes['TYPE']&& strtolower(trim($element->getAttribute('TYPE'))) == 'json'{
  200.             if (!isset($element->ucAttributes['NAME'])) {
  201.                 return $this->compiler->appendHTML("<B>Flexy:Include json without a name=jsname (Line: {$element->line})</B>");
  202.             }
  203.             
  204.             $arg = "'"$element->getAttribute('SRC')."'";
  205.             $name $element->getAttribute('NAME');
  206.             return  
  207.                 $this->compiler->appendHTML("\n<script type='text/javascript'>\n"
  208.                 $this->compiler->appendPHP"\n".
  209.                     "\$x = new HTML_Template_Flexy(\$this->options);\n".
  210.                     " echo 'var ' . {$name} .' = '. ".
  211.                         "json_encode( file_get_contents ( \$x->resolvePath( {$arg} ) .'/'. {$arg})) . \";\n\";\n"
  212.  
  213.                 ) .
  214.                 $this->compiler->appendHTML("\n</script>\n")  ;
  215.                 
  216.             
  217.         }
  218.         
  219.         
  220.          
  221.         // it's a string so its easy to handle
  222.         switch (true) {
  223.             case is_string($arg):
  224.                 if ($arg == '""') {
  225.                     return $this->compiler->appendHTML("<B>Flexy:Include src attribute is empty. (Line: {$element->line})</B>");
  226.                 }
  227.                 $arg = "'"$element->getAttribute('SRC')."'";
  228.                 break;
  229.             
  230.             case is_array($arg)// it's an array -> strings and variables possible
  231.                 $string '"';
  232.                 foreach($arg as $item{
  233.                     //it's a string
  234.                     if (is_string($item)) {
  235.                         if ($item != '' && $item != '"' && $item != '""' && 
  236.                             $item != "''") {
  237.                             $string .= $item;
  238.                         }
  239.                     } else {
  240.                         //it's a variable
  241.                         if (is_object($item) && is_a($item, 'HTML_Template_Flexy_Token_Var')) {
  242.                             $value = $item->toVar($item->value);
  243.                             if (is_object($value&& is_a($value'PEAR_Error')) {
  244.                                 return $value;
  245.                             }
  246.                             $string .= "{{$value}}";
  247.                         }
  248.                     }
  249.                 }
  250.                 $arg = $string . '"';
  251.                 break;
  252.             
  253.             default:
  254.             //something unexspected
  255.                 return HTML_Template_Flexy::staticRaiseError(
  256.                     ' Flexy:Include SRC needs a string or variable/method as value. '.
  257.                     " Error on Line {$element->line} &lt;{$element->tag}&gt;",
  258.                     null, HTML_TEMPLATE_FLEXY_ERROR_DIE)
  259.             
  260.                 
  261.             
  262.         }
  263.  
  264.         // ideally it would be nice to embed the results of one template into another.
  265.         // however that would involve some complex test which would have to stat
  266.         // the child templates anyway..
  267.         // compile the child template....
  268.         // output... include $this->options['compiled_templates'] . $arg . $this->options['locale'] . '.php'
  269.         return $this->compiler->appendPHP"\n".
  270.                 "\$x = new HTML_Template_Flexy(\$this->options);\n".
  271.                 "\$x->compile({$arg});\n".
  272.                 "\$_t = function_exists('clone') ? clone(\$t) : \$t;\n".
  273.                 "foreach(".$element->scopeVarsToArrayString()"  as \$k) {\n" .
  274.                 "    if (\$k != 't') { \$_t->\$k = \$\$k; }\n" .
  275.                 "}\n" .
  276.                 "\$x->outputObject(\$_t, \$this->elements);\n"
  277.             );
  278.     
  279.     }
  280.     
  281.     /**
  282.     * Convert flexy tokens to HTML_Template_Flexy_Elements.
  283.     *
  284.     * @param    object token to convert into a element.
  285.     * @return   object HTML_Template_Flexy_Element
  286.     * @access   public
  287.     */
  288.     function toElement($element) 
  289.     {
  290.        return '';
  291.     }
  292.         
  293.     
  294.     /**
  295.     * Handler for User defined functions in templates..
  296.     * <flexy:function name="xxxxx">.... </flexy:block>  // equivilant to function xxxxx() { 
  297.     * <flexy:function call="{xxxxx}">.... </flexy:block>  // equivilant to function {$xxxxx}() { 
  298.     * <flexy:function call="xxxxx">.... </flexy:block>  // equivilant to function {$xxxxx}() { 
  299.     * 
  300.     * This will not handle nested blocks initially!! (and may cause even more problems with 
  301.     * if /foreach stuff..!!
  302.     *
  303.     * @param    object token to convert into a element.
  304.     * @access   public
  305.     */
  306.   
  307.     
  308.     function functionToString($element) 
  309.     {
  310.         
  311.         if ($arg = $element->getAttribute('NAME')) {
  312.             // this is a really kludgy way of doing this!!!
  313.             // hopefully the new Template Package will have a sweeter method..
  314.             $GLOBALS['_HTML_TEMPLATE_FLEXY']['prefixOutput']  .= 
  315.                 $this->compiler->appendPHP
  316.                     "\nfunction _html_template_flexy_compiler_flexy_flexy_{$arg}(\$t,\$this) {\n").
  317.                 $element->compileChildren($this->compiler.
  318.                 $this->compiler->appendPHP"\n}\n");
  319.                 
  320.                 return '';
  321.         }
  322.         if (!isset($element->ucAttributes['CALL'])) {
  323.             
  324.             return HTML_Template_Flexy::staticRaiseError(
  325.                 ' tag flexy:function needs an argument call or name'.
  326.                 " Error on Line {$element->line} &lt;{$element->tag}&gt;",
  327.                          null,   HTML_TEMPLATE_FLEXY_ERROR_DIE);
  328.         }
  329.         // call is a  stirng : nice and simple..
  330.         if (is_string($element->ucAttributes['CALL'])) {
  331.             $arg = $element->getAttribute('CALL');
  332.             return $this->compiler->appendPHP
  333.                     "if (function_exists('_html_template_flexy_compiler_flexy_flexy_{$arg}')) " .
  334.                     " _html_template_flexy_compiler_flexy_flexy_{$arg}(\$t,\$this);");
  335.         }
  336.         
  337.         // we make a big assumption here.. - it should really be error checked..
  338.         // that the {xxx} element is item 1 in the list... 
  339.         $e=$element->ucAttributes['CALL'][1];
  340.         $add $e->toVar($e->value);
  341.         if (is_object($add&& is_a($add,'PEAR_Error')) {
  342.             return $add;
  343.         } 
  344.         return $this->compiler->appendPHP(
  345.             "if (function_exists('_html_template_flexy_compiler_flexy_flexy_'.{$add})) ".
  346.             "call_user_func_array('_html_template_flexy_compiler_flexy_flexy_'.{$add},array(\$t,\$this));");
  347.         
  348.         
  349.         
  350.     }
  351.  
  352.  
  353.     
  354.     
  355.    /** 
  356.     /**
  357.     * - A partial is a subtemplate to which you can pass variables.
  358.     * - You can define variables for the partial as xml attributes
  359.     * - You can provide context for the variables by adhering to the 
  360.     *   convention 'subtemplateVarName="templateVarName"'
  361.     * - See example below:
  362.     *  
  363.     * <flexy:partial src="test.html" subtemplateVar1="var1" 
  364.     *   subtemplateVar2="object.var2" subtemplateVar3="#literal1#" />
  365.     */
  366.     function partialToString($element)
  367.     {
  368.         $src = $element->getAttribute('SRC');
  369.         
  370.         if (!$src)  {
  371.             return $this->compiler->appendHTML("<B>Flexy:Subtemplate without a src=filename</B>");
  372.         }
  373.         
  374.         /**
  375.         * Define parameters for partial (if set)
  376.         */
  377.         $aAttribute = $element->getAttributes()
  378.  
  379.         
  380.         if (!is_array($aAttribute)) {
  381.             $aAttribute = array();
  382.         }
  383.         
  384.         $aOutput = array();
  385.             
  386.         foreach ($aAttribute as $name => $value) {
  387.             if ($name == 'src' || $name == '/') {
  388.                 continue;
  389.             }
  390.                 
  391.             $varName                = trim($name);
  392.             $varVal                 = trim($value);
  393.             $isLiteral              = preg_match('@\#(.*)\#@',$varVal);
  394.  
  395.             /**
  396.             *   Provide supplied variables with subtemplate context
  397.             * - Deal with string literals (enclosed in # tags - flexy
  398.             * hack/convention).
  399.             * - Variable binding: Look in output object scope first, then
  400.             * template scope.
  401.             */
  402.             if (!$isLiteral) {
  403.                 $varVal             = str_replace('.','->',trim($value));
  404.                 $varVal             = '(isset($t->' . $varVal')) ? $t->' . $varVal .' : $'$varVal;
  405.             } else  {
  406.                 $varVal             = preg_replace('@\#(.*)\#@','"\1"',$varVal);
  407.             }
  408.                 
  409.             $aOutput[$varName]      = $varVal;
  410.         }
  411.  
  412.         $varsOutput = "\n\$oOutput = clone \$t;\n";
  413.  
  414.         foreach ($aOutput as $key=>$val) {
  415.             $varsOutput .= "\$oOutput->{$key} = {$val};\n";
  416.         }
  417.         
  418.         
  419.         
  420.         /**
  421.         * Pass code to compiler
  422.         */
  423.         return $this->compiler->appendPHP 
  424.                 "
  425.                 \$x = new HTML_Template_Flexy(\$this->options);
  426.                 \$x->compile('{$src}');
  427.                 {$varsOutput}
  428.                 \$x->outputObject(\$oOutput, \$this->elements);
  429.                 "
  430.         );
  431.     } 
  432.     
  433.     
  434.     
  435.     

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