4 * Smarty Internal Plugin Compile extend
5 * Compiles the {extends} tag
13 * Smarty Internal Plugin Compile extend Class
16 * @subpackage Compiler
18 class Smarty_Internal_Compile_Extends extends Smarty_Internal_Compile_Shared_Inheritance
21 * Attribute definition: Overwrites base class.
24 * @see Smarty_Internal_CompileBase
26 public $required_attributes = array('file');
29 * Array of names of optional attribute required by tag
30 * use array('_any') if there is no restriction of attributes names
34 public $optional_attributes = array('extends_resource');
37 * Attribute definition: Overwrites base class.
40 * @see Smarty_Internal_CompileBase
42 public $shorttag_order = array('file');
45 * Compiles code for the {extends} tag extends: resource
47 * @param array $args array with attributes from parser
48 * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
50 * @return string compiled code
51 * @throws \SmartyCompilerException
52 * @throws \SmartyException
54 public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
56 // check and get attributes
57 $_attr = $this->getAttributes($compiler, $args);
58 if ($_attr[ 'nocache' ] === true) {
59 $compiler->trigger_template_error('nocache option not allowed', $compiler->parser->lex->line - 1);
61 if (strpos($_attr[ 'file' ], '$_tmp') !== false) {
62 $compiler->trigger_template_error('illegal value for file attribute', $compiler->parser->lex->line - 1);
64 // add code to initialize inheritance
65 $this->registerInit($compiler, true);
66 $file = trim($_attr[ 'file' ], '\'"');
67 if (strlen($file) > 8 && substr($file, 0, 8) == 'extends:') {
68 // generate code for each template
69 $files = array_reverse(explode('|', substr($file, 8)));
71 foreach ($files as $file) {
72 if ($file[ 0 ] == '"') {
73 $file = trim($file, '".');
78 if ($i == count($files) && isset($_attr[ 'extends_resource' ])) {
79 $this->compileEndChild($compiler);
81 $this->compileInclude($compiler, $file);
83 if (!isset($_attr[ 'extends_resource' ])) {
84 $this->compileEndChild($compiler);
87 $this->compileEndChild($compiler, $_attr[ 'file' ]);
89 $compiler->has_code = false;
94 * Add code for inheritance endChild() method to end of template
96 * @param \Smarty_Internal_TemplateCompilerBase $compiler
97 * @param null|string $template optional inheritance parent template
99 private function compileEndChild(Smarty_Internal_TemplateCompilerBase $compiler, $template = null)
102 if (isset($template) && $compiler->smarty->merge_compiled_includes) {
103 $code = $compiler->compileTag('include', array($template, array('scope' => 'parent')));
104 if (preg_match("/([,][\s]*['][a-z0-9]+['][,][\s]*[']content.*['])[)]/", $code, $match)) {
105 $inlineUids = $match[ 1 ];
108 $compiler->parser->template_postfix[] = new Smarty_Internal_ParseTree_Tag($compiler->parser,
109 "<?php \$_smarty_tpl->inheritance->endChild(\$_smarty_tpl" .
111 ', ' . $template . $inlineUids :
116 * Add code for including subtemplate to end of template
118 * @param \Smarty_Internal_TemplateCompilerBase $compiler
119 * @param string $template subtemplate name
121 private function compileInclude(Smarty_Internal_TemplateCompilerBase $compiler, $template)
123 $compiler->parser->template_postfix[] = new Smarty_Internal_ParseTree_Tag($compiler->parser,
124 $compiler->compileTag('include',
126 array('scope' => 'parent'))));
130 * Create source code for {extends} from source components array
132 * @param []\Smarty_Internal_Template_Source $components
136 public static function extendsSourceArrayCode($components)
138 $resources = array();
139 foreach ($components as $source) {
140 $resources[] = $source->resource;
142 return '{extends file=\'extends:' . join('|', $resources) . '\' extends_resource=true}';