library/smarty/libs/sysplugins/smarty_internal_compile_extends.php
changeset 46 f11c31f7fa3e
parent 45 a56e7f9a0463
child 47 03388ec805b4
equal deleted inserted replaced
45:a56e7f9a0463 46:f11c31f7fa3e
     1 <?php
       
     2 
       
     3 /**
       
     4  * Smarty Internal Plugin Compile extend
       
     5  * Compiles the {extends} tag
       
     6  *
       
     7  * @package    Smarty
       
     8  * @subpackage Compiler
       
     9  * @author     Uwe Tews
       
    10  */
       
    11 
       
    12 /**
       
    13  * Smarty Internal Plugin Compile extend Class
       
    14  *
       
    15  * @package    Smarty
       
    16  * @subpackage Compiler
       
    17  */
       
    18 class Smarty_Internal_Compile_Extends extends Smarty_Internal_CompileBase {
       
    19     /**
       
    20      * Attribute definition: Overwrites base class.
       
    21      *
       
    22      * @var array
       
    23      * @see Smarty_Internal_CompileBase
       
    24      */
       
    25     public $required_attributes = array('file');
       
    26     /**
       
    27      * Attribute definition: Overwrites base class.
       
    28      *
       
    29      * @var array
       
    30      * @see Smarty_Internal_CompileBase
       
    31      */
       
    32     public $shorttag_order = array('file');
       
    33 
       
    34     /**
       
    35      * Compiles code for the {extends} tag
       
    36      *
       
    37      * @param array $args array with attributes from parser
       
    38      * @param object $compiler compiler object
       
    39      *
       
    40      * @return string compiled code
       
    41      */
       
    42     public function compile($args, $compiler) {
       
    43         // check and get attributes
       
    44         $_attr = $this->getAttributes($compiler, $args);
       
    45         if ($_attr['nocache'] === true) {
       
    46             $compiler->trigger_template_error('nocache option not allowed', $compiler->lex->taglineno);
       
    47         }
       
    48         if (strpos($_attr['file'], '$_tmp') !== false) {
       
    49             $compiler->trigger_template_error('illegal value for file attribute', $compiler->lex->taglineno);
       
    50         }
       
    51 
       
    52         $name = $_attr['file'];
       
    53         /** @var Smarty_Internal_Template $_smarty_tpl
       
    54          * used in evaluated code
       
    55          */
       
    56         $_smarty_tpl = $compiler->template;
       
    57         eval("\$tpl_name = $name;");
       
    58         // create template object
       
    59         $_template = new $compiler->smarty->template_class($tpl_name, $compiler->smarty, $compiler->template);
       
    60         // check for recursion
       
    61         $uid = $_template->source->uid;
       
    62         if (isset($compiler->extends_uid[$uid])) {
       
    63             $compiler->trigger_template_error("illegal recursive call of \"$include_file\"", $compiler->lex->line - 1);
       
    64         }
       
    65         $compiler->extends_uid[$uid] = true;
       
    66         if (empty($_template->source->components)) {
       
    67             array_unshift($compiler->sources, $_template->source);
       
    68         } else {
       
    69             foreach ($_template->source->components as $source) {
       
    70                 array_unshift($compiler->sources, $source);
       
    71                 $uid = $source->uid;
       
    72                 if (isset($compiler->extends_uid[$uid])) {
       
    73                     $compiler->trigger_template_error("illegal recursive call of \"{$source->filepath}\"", $compiler->lex->line - 1);
       
    74                 }
       
    75                 $compiler->extends_uid[$uid] = true;
       
    76             }
       
    77         }
       
    78         unset ($_template);
       
    79         $compiler->inheritance_child = true;
       
    80         $compiler->lex->yypushstate(Smarty_Internal_Templatelexer::CHILDBODY);
       
    81         return '';
       
    82     }
       
    83 }