4 * Smarty Internal Plugin Compile extend
6 * Compiles the {extends} tag
14 * Smarty Internal Plugin Compile extend Class
17 * @subpackage Compiler
19 class Smarty_Internal_Compile_Extends extends Smarty_Internal_CompileBase {
22 * Attribute definition: Overwrites base class.
25 * @see Smarty_Internal_CompileBase
27 public $required_attributes = array('file');
29 * Attribute definition: Overwrites base class.
32 * @see Smarty_Internal_CompileBase
34 public $shorttag_order = array('file');
36 * mbstring.overload flag
40 public $mbstring_overload = 0;
43 * Compiles code for the {extends} tag
45 * @param array $args array with attributes from parser
46 * @param object $compiler compiler object
47 * @return string compiled code
49 public function compile($args, $compiler)
51 static $_is_stringy = array('string' => true, 'eval' => true);
52 $this->_rdl = preg_quote($compiler->smarty->right_delimiter);
53 $this->_ldl = preg_quote($compiler->smarty->left_delimiter);
54 $filepath = $compiler->template->source->filepath;
55 $this->mbstring_overload = ini_get('mbstring.func_overload') & 2;
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->lex->taglineno);
62 $_smarty_tpl = $compiler->template;
64 if (strpos($_attr['file'], '$_tmp') !== false) {
65 $compiler->trigger_template_error('illegal value for file attribute', $compiler->lex->taglineno);
67 eval('$include_file = ' . $_attr['file'] . ';');
68 // create template object
69 $_template = new $compiler->smarty->template_class($include_file, $compiler->smarty, $compiler->template);
70 // save file dependency
71 if (isset($_is_stringy[$_template->source->type])) {
72 $template_sha1 = sha1($include_file);
74 $template_sha1 = sha1($_template->source->filepath);
76 if (isset($compiler->template->properties['file_dependency'][$template_sha1])) {
77 $compiler->trigger_template_error("illegal recursive call of \"{$include_file}\"", $compiler->lex->line - 1);
79 $compiler->template->properties['file_dependency'][$template_sha1] = array($_template->source->filepath, $_template->source->timestamp, $_template->source->type);
80 $_content = ($this->mbstring_overload ? mb_substr($compiler->template->source->content, $compiler->lex->counter - 1, 20000000, 'latin1') : substr($compiler->template->source->content, $compiler->lex->counter - 1));
81 if (preg_match_all("!({$this->_ldl}block\s(.+?){$this->_rdl})!", $_content, $s) !=
82 preg_match_all("!({$this->_ldl}/block{$this->_rdl})!", $_content, $c)) {
83 $compiler->trigger_template_error('unmatched {block} {/block} pairs');
85 preg_match_all("!{$this->_ldl}block\s(.+?){$this->_rdl}|{$this->_ldl}/block{$this->_rdl}|{$this->_ldl}\*([\S\s]*?)\*{$this->_rdl}!", $_content, $_result, PREG_OFFSET_CAPTURE);
86 $_result_count = count($_result[0]);
88 while ($_start+1 < $_result_count) {
91 if (($this->mbstring_overload ? mb_substr($_result[0][$_start][0],0,mb_strlen($compiler->smarty->left_delimiter,'latin1')+1, 'latin1') : substr($_result[0][$_start][0],0,strlen($compiler->smarty->left_delimiter)+1)) == $compiler->smarty->left_delimiter.'*') {
95 while ($_level != 0) {
97 if (($this->mbstring_overload ? mb_substr($_result[0][$_start + $_end][0],0,mb_strlen($compiler->smarty->left_delimiter,'latin1')+1, 'latin1') : substr($_result[0][$_start + $_end][0],0,strlen($compiler->smarty->left_delimiter)+1)) == $compiler->smarty->left_delimiter.'*') {
100 if (!strpos($_result[0][$_start + $_end][0], '/')) {
106 $_block_content = str_replace($compiler->smarty->left_delimiter . '$smarty.block.parent' . $compiler->smarty->right_delimiter, '%%%%SMARTY_PARENT%%%%',
107 ($this->mbstring_overload ? mb_substr($_content, $_result[0][$_start][1] + mb_strlen($_result[0][$_start][0], 'latin1'), $_result[0][$_start + $_end][1] - $_result[0][$_start][1] - + mb_strlen($_result[0][$_start][0], 'latin1'), 'latin1') : 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]))));
108 Smarty_Internal_Compile_Block::saveBlockData($_block_content, $_result[0][$_start][0], $compiler->template, $filepath);
109 $_start = $_start + $_end + 1;
111 if ($_template->source->type == 'extends') {
112 $_template->block_data = $compiler->template->block_data;
114 $compiler->template->source->content = $_template->source->content;
115 if ($_template->source->type == 'extends') {
116 $compiler->template->block_data = $_template->block_data;
117 foreach ($_template->source->components as $key => $component) {
118 $compiler->template->properties['file_dependency'][$key] = array($component->filepath, $component->timestamp, $component->type);
121 $compiler->template->source->filepath = $_template->source->filepath;
122 $compiler->abort_and_recompile = true;