1 <?php
3 /**
4 * Smarty Internal Plugin Compile extend
5 *
6 * Compiles the {extends} tag
7 *
8 * @package Smarty
9 * @subpackage Compiler
10 * @author Uwe Tews
11 */
13 /**
14 * Smarty Internal Plugin Compile extend Class
15 */
16 class Smarty_Internal_Compile_Extends extends Smarty_Internal_CompileBase {
17 // attribute definitions
18 public $required_attributes = array('file');
19 public $shorttag_order = array('file');
21 /**
22 * Compiles code for the {extends} tag
23 *
24 * @param array $args array with attributes from parser
25 * @param object $compiler compiler object
26 * @return string compiled code
27 */
28 public function compile($args, $compiler)
29 {
30 $this->compiler = $compiler;
31 $this->smarty = $compiler->smarty;
32 $this->_rdl = preg_quote($this->smarty->right_delimiter);
33 $this->_ldl = preg_quote($this->smarty->left_delimiter);
34 $filepath = $compiler->template->getTemplateFilepath();
35 // check and get attributes
36 $_attr = $this->_get_attributes($args);
37 if ($_attr['nocache'] === true) {
38 $this->compiler->trigger_template_error('nocache option not allowed', $this->compiler->lex->taglineno);
39 }
41 $_smarty_tpl = $compiler->template;
42 $include_file = null;
43 if (strpos($_attr['file'],'$_tmp') !== false || strpos($_attr['file'],'$_smarty_tpl') !== false || strpos($_attr['file'],'::') !== false) {
44 $this->compiler->trigger_template_error('a variable file attribute is illegal', $this->compiler->lex->taglineno);
45 }
46 eval('$include_file = ' . $_attr['file'] . ';');
47 // create template object
48 $_template = new $compiler->smarty->template_class($include_file, $this->smarty, $compiler->template);
49 // save file dependency
50 if (in_array($_template->resource_type,array('eval','string'))) {
51 $template_sha1 = sha1($include_file);
52 } else {
53 $template_sha1 = sha1($_template->getTemplateFilepath());
54 }
55 if (isset($compiler->template->properties['file_dependency'][$template_sha1])) {
56 $this->compiler->trigger_template_error("illegal recursive call of \"{$include_file}\"",$compiler->lex->line-1);
57 }
58 $compiler->template->properties['file_dependency'][$template_sha1] = array($_template->getTemplateFilepath(), $_template->getTemplateTimestamp(),$_template->resource_type);
59 $_content = substr($compiler->template->template_source,$compiler->lex->counter-1);
60 if (preg_match_all("!({$this->_ldl}block\s(.+?){$this->_rdl})!", $_content, $s) !=
61 preg_match_all("!({$this->_ldl}/block{$this->_rdl})!", $_content, $c)) {
62 $this->compiler->trigger_template_error('unmatched {block} {/block} pairs');
63 }
64 preg_match_all("!{$this->_ldl}block\s(.+?){$this->_rdl}|{$this->_ldl}/block{$this->_rdl}!", $_content, $_result, PREG_OFFSET_CAPTURE);
65 $_result_count = count($_result[0]);
66 $_start = 0;
67 while ($_start < $_result_count) {
68 $_end = 0;
69 $_level = 1;
70 while ($_level != 0) {
71 $_end++;
72 if (!strpos($_result[0][$_start + $_end][0], '/')) {
73 $_level++;
74 } else {
75 $_level--;
76 }
77 }
78 $_block_content = str_replace($this->smarty->left_delimiter . '$smarty.block.parent' . $this->smarty->right_delimiter, '%%%%SMARTY_PARENT%%%%',
79 substr($_content, $_result[0][$_start][1] + strlen($_result[0][$_start][0]), $_result[0][$_start + $_end][1] - $_result[0][$_start][1] - + strlen($_result[0][$_start][0])));
80 Smarty_Internal_Compile_Block::saveBlockData($_block_content, $_result[0][$_start][0], $compiler->template, $filepath);
81 $_start = $_start + $_end + 1;
82 }
83 $compiler->template->template_source = $_template->getTemplateSource();
84 $compiler->template->template_filepath = $_template->getTemplateFilepath();
85 $compiler->abort_and_recompile = true;
86 return '';
87 }
89 }
90 ?>