2009-03-22 16:09:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2010-02-12 01:14:34 +00:00
|
|
|
* Smarty Internal Plugin Compile extend
|
|
|
|
*
|
|
|
|
* Compiles the {extends} tag
|
|
|
|
*
|
|
|
|
* @package Smarty
|
|
|
|
* @subpackage Compiler
|
|
|
|
* @author Uwe Tews
|
|
|
|
*/
|
2009-03-22 16:09:05 +00:00
|
|
|
/**
|
2010-02-12 01:14:34 +00:00
|
|
|
* Smarty Internal Plugin Compile extend Class
|
|
|
|
*/
|
2009-11-17 19:03:06 +00:00
|
|
|
class Smarty_Internal_Compile_Extends extends Smarty_Internal_CompileBase {
|
2009-03-22 16:09:05 +00:00
|
|
|
/**
|
2010-02-12 01:14:34 +00:00
|
|
|
* Compiles code for the {extends} tag
|
|
|
|
*
|
|
|
|
* @param array $args array with attributes from parser
|
|
|
|
* @param object $compiler compiler object
|
|
|
|
* @return string compiled code
|
|
|
|
*/
|
2009-03-22 16:09:05 +00:00
|
|
|
public function compile($args, $compiler)
|
|
|
|
{
|
|
|
|
$this->compiler = $compiler;
|
2009-11-30 19:56:17 +00:00
|
|
|
$this->smarty = $compiler->smarty;
|
2010-01-06 18:48:50 +00:00
|
|
|
$this->_rdl = preg_quote($this->smarty->right_delimiter);
|
|
|
|
$this->_ldl = preg_quote($this->smarty->left_delimiter);
|
2009-03-22 16:09:05 +00:00
|
|
|
$this->required_attributes = array('file');
|
|
|
|
// check and get attributes
|
|
|
|
$_attr = $this->_get_attributes($args);
|
2009-04-12 05:40:30 +00:00
|
|
|
$_smarty_tpl = $compiler->template;
|
2009-04-12 02:30:54 +00:00
|
|
|
// $include_file = '';
|
|
|
|
eval('$include_file = ' . $_attr['file'] . ';');
|
2009-03-22 16:09:05 +00:00
|
|
|
// create template object
|
2009-12-01 22:24:31 +00:00
|
|
|
$_template = new $compiler->smarty->template_class($include_file, $this->smarty, $compiler->template);
|
2009-03-22 16:09:05 +00:00
|
|
|
// save file dependency
|
2010-07-06 20:00:41 +00:00
|
|
|
$template_sha1 = sha1($_template->getTemplateFilepath());
|
|
|
|
if (isset($compiler->template->properties['file_dependency'][$template_sha1])) {
|
|
|
|
$this->compiler->trigger_template_error("illegal recursive call of \"{$include_file}\"",$compiler->lex->line-1);
|
|
|
|
}
|
|
|
|
$compiler->template->properties['file_dependency'][$template_sha1] = array($_template->getTemplateFilepath(), $_template->getTemplateTimestamp());
|
2010-02-12 15:56:17 +00:00
|
|
|
$_content = $compiler->template->template_source;
|
2010-04-30 18:11:34 +00:00
|
|
|
if (preg_match_all("!({$this->_ldl}block\s(.+?){$this->_rdl})!", $_content, $s) !=
|
2010-02-12 15:56:17 +00:00
|
|
|
preg_match_all("!({$this->_ldl}/block(.*?){$this->_rdl})!", $_content, $c)) {
|
2010-01-06 18:48:50 +00:00
|
|
|
$this->compiler->trigger_template_error('unmatched {block} {/block} pairs');
|
2009-07-20 15:38:11 +00:00
|
|
|
}
|
2010-04-30 18:11:34 +00:00
|
|
|
preg_match_all("!{$this->_ldl}block\s(.+?){$this->_rdl}|{$this->_ldl}/block(.*?){$this->_rdl}!", $_content, $_result, PREG_OFFSET_CAPTURE);
|
2010-02-12 01:14:34 +00:00
|
|
|
$_result_count = count($_result[0]);
|
2010-02-12 15:56:17 +00:00
|
|
|
$_start = 0;
|
|
|
|
while ($_start < $_result_count) {
|
|
|
|
$_end = 0;
|
|
|
|
$_level = 1;
|
|
|
|
while ($_level != 0) {
|
|
|
|
$_end++;
|
|
|
|
if (!strpos($_result[0][$_start + $_end][0], '/')) {
|
|
|
|
$_level++;
|
|
|
|
} else {
|
|
|
|
$_level--;
|
|
|
|
}
|
2010-02-12 01:14:34 +00:00
|
|
|
}
|
2010-02-12 15:56:17 +00:00
|
|
|
$_block_content = str_replace($this->smarty->left_delimiter . '$smarty.block.parent' . $this->smarty->right_delimiter, '%%%%SMARTY_PARENT%%%%',
|
|
|
|
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])));
|
|
|
|
$this->saveBlockData($_block_content, $_result[0][$_start][0], $compiler->template);
|
|
|
|
$_start = $_start + $_end + 1;
|
2009-09-10 23:08:06 +00:00
|
|
|
}
|
2009-03-22 16:09:05 +00:00
|
|
|
$compiler->template->template_source = $_template->getTemplateSource();
|
2009-12-03 19:49:17 +00:00
|
|
|
$compiler->template->template_filepath = $_template->getTemplateFilepath();
|
2009-03-22 16:09:05 +00:00
|
|
|
$compiler->abort_and_recompile = true;
|
2010-07-30 13:09:52 +00:00
|
|
|
return '';
|
2009-03-22 16:09:05 +00:00
|
|
|
}
|
|
|
|
|
2009-11-30 19:56:17 +00:00
|
|
|
protected function saveBlockData($block_content, $block_tag, $template)
|
2009-03-22 16:09:05 +00:00
|
|
|
{
|
2010-01-06 18:48:50 +00:00
|
|
|
if (0 == preg_match("!(.?)(name=)(.*?)(?=(\s|{$this->_rdl}))!", $block_tag, $_match)) {
|
2009-09-10 23:08:06 +00:00
|
|
|
$this->compiler->trigger_template_error("\"" . $block_tag . "\" missing name attribute");
|
2009-03-22 16:09:05 +00:00
|
|
|
} else {
|
2010-07-06 20:00:41 +00:00
|
|
|
$_name = trim($_match[3], '\'"');
|
|
|
|
// replace {$smarty.block.child}
|
2010-02-17 21:30:36 +00:00
|
|
|
if (strpos($block_content, $this->smarty->left_delimiter . '$smarty.block.child' . $this->smarty->right_delimiter) !== false) {
|
|
|
|
if (isset($this->smarty->block_data[$_name])) {
|
|
|
|
$block_content = str_replace($this->smarty->left_delimiter . '$smarty.block.child' . $this->smarty->right_delimiter,
|
|
|
|
$this->smarty->block_data[$_name]['source'], $block_content);
|
|
|
|
unset($this->smarty->block_data[$_name]);
|
|
|
|
} else {
|
|
|
|
$block_content = str_replace($this->smarty->left_delimiter . '$smarty.block.child' . $this->smarty->right_delimiter,
|
|
|
|
'', $block_content);
|
|
|
|
}
|
|
|
|
}
|
2009-10-12 14:37:31 +00:00
|
|
|
if (isset($this->smarty->block_data[$_name])) {
|
2009-12-03 19:49:17 +00:00
|
|
|
if (strpos($this->smarty->block_data[$_name]['source'], '%%%%SMARTY_PARENT%%%%') !== false) {
|
|
|
|
$this->smarty->block_data[$_name]['source'] =
|
|
|
|
str_replace('%%%%SMARTY_PARENT%%%%', $block_content, $this->smarty->block_data[$_name]['source']);
|
2009-10-12 14:37:31 +00:00
|
|
|
} elseif ($this->smarty->block_data[$_name]['mode'] == 'prepend') {
|
2009-12-03 19:49:17 +00:00
|
|
|
$this->smarty->block_data[$_name]['source'] .= $block_content;
|
2009-10-12 14:37:31 +00:00
|
|
|
} elseif ($this->smarty->block_data[$_name]['mode'] == 'append') {
|
2009-12-03 19:49:17 +00:00
|
|
|
$this->smarty->block_data[$_name]['source'] = $block_content . $this->smarty->block_data[$_name]['source'];
|
2009-04-12 22:26:02 +00:00
|
|
|
}
|
|
|
|
} else {
|
2009-12-03 19:49:17 +00:00
|
|
|
$this->smarty->block_data[$_name]['source'] = $block_content;
|
2009-03-22 16:09:05 +00:00
|
|
|
}
|
2009-12-12 10:50:40 +00:00
|
|
|
if (preg_match('/(.?)(append)(.*)/', $block_tag, $_match) != 0) {
|
2009-10-12 14:37:31 +00:00
|
|
|
$this->smarty->block_data[$_name]['mode'] = 'append';
|
2009-12-12 10:50:40 +00:00
|
|
|
} elseif (preg_match('/(.?)(prepend)(.*)/', $block_tag, $_match) != 0) {
|
2009-10-12 14:37:31 +00:00
|
|
|
$this->smarty->block_data[$_name]['mode'] = 'prepend';
|
2009-04-12 22:26:02 +00:00
|
|
|
} else {
|
2009-10-12 14:37:31 +00:00
|
|
|
$this->smarty->block_data[$_name]['mode'] = 'replace';
|
2009-04-12 22:26:02 +00:00
|
|
|
}
|
2009-12-03 19:49:17 +00:00
|
|
|
$this->smarty->block_data[$_name]['file'] = $template->getTemplateFilepath();
|
2009-03-22 16:09:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-12 01:14:34 +00:00
|
|
|
?>
|