2009-11-06 14:35:00 +00:00
|
|
|
<?php
|
2015-09-01 01:54:28 +02:00
|
|
|
/*
|
|
|
|
* This file is part of Smarty.
|
2013-07-27 07:05:26 +00:00
|
|
|
*
|
2015-09-01 01:54:28 +02:00
|
|
|
* (c) 2015 Uwe Tews
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2013-07-27 07:05:26 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Smarty Internal Plugin Compile Block Class
|
|
|
|
*
|
2015-09-01 01:54:28 +02:00
|
|
|
* @author Uwe Tews <uwe.tews@googlemail.com>
|
2013-07-27 07:05:26 +00:00
|
|
|
*/
|
|
|
|
class Smarty_Internal_Compile_Block extends Smarty_Internal_CompileBase
|
|
|
|
{
|
2011-12-20 17:50:15 +00:00
|
|
|
/**
|
2013-07-27 07:05:26 +00:00
|
|
|
* Attribute definition: Overwrites base class.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @see Smarty_Internal_CompileBase
|
|
|
|
*/
|
|
|
|
public $required_attributes = array('name');
|
2012-06-30 15:53:22 +00:00
|
|
|
|
2011-12-20 17:50:15 +00:00
|
|
|
/**
|
2013-07-27 07:05:26 +00:00
|
|
|
* Attribute definition: Overwrites base class.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @see Smarty_Internal_CompileBase
|
|
|
|
*/
|
2013-08-24 18:46:31 +00:00
|
|
|
public $shorttag_order = array('name');
|
2012-06-30 15:53:22 +00:00
|
|
|
|
2013-07-27 07:05:26 +00:00
|
|
|
/**
|
|
|
|
* Attribute definition: Overwrites base class.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @see Smarty_Internal_CompileBase
|
|
|
|
*/
|
2015-09-01 01:54:28 +02:00
|
|
|
public $option_flags = array('hide', 'nocache');
|
2013-08-24 18:46:31 +00:00
|
|
|
|
|
|
|
/**
|
2015-09-01 01:54:28 +02:00
|
|
|
* Attribute definition: Overwrites base class.
|
2013-08-24 18:46:31 +00:00
|
|
|
*
|
|
|
|
* @var array
|
2015-09-01 01:54:28 +02:00
|
|
|
* @see Smarty_Internal_CompileBase
|
2013-08-24 18:46:31 +00:00
|
|
|
*/
|
2015-09-01 01:54:28 +02:00
|
|
|
public $optional_attributes = array();
|
2013-08-24 18:46:31 +00:00
|
|
|
|
|
|
|
/**
|
2015-09-01 01:54:28 +02:00
|
|
|
* nesting level of block tags
|
2013-08-24 18:46:31 +00:00
|
|
|
*
|
2015-09-01 01:54:28 +02:00
|
|
|
* @var int
|
2013-08-24 18:46:31 +00:00
|
|
|
*/
|
2015-09-01 01:54:28 +02:00
|
|
|
public static $blockTagNestingLevel = 0;
|
2011-09-16 14:19:56 +00:00
|
|
|
|
2013-07-27 07:05:26 +00:00
|
|
|
/**
|
|
|
|
* Compiles code for the {block} tag
|
|
|
|
*
|
2015-09-01 01:54:28 +02:00
|
|
|
* @param array $args array with attributes from parser
|
|
|
|
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
|
|
|
|
* @param array $parameter array with compilation parameter
|
2014-06-06 02:40:04 +00:00
|
|
|
*
|
2015-07-01 03:25:55 +02:00
|
|
|
* @return bool true
|
2013-07-27 07:05:26 +00:00
|
|
|
*/
|
2015-09-01 01:54:28 +02:00
|
|
|
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
|
2013-07-27 07:05:26 +00:00
|
|
|
{
|
2015-09-14 23:46:17 +02:00
|
|
|
if ($compiler->blockTagNestingLevel == 0 && $compiler->inheritanceChild) {
|
2015-09-01 01:54:28 +02:00
|
|
|
$this->option_flags = array('hide', 'nocache', 'append', 'prepend');
|
|
|
|
} else {
|
|
|
|
$this->option_flags = array('hide', 'nocache');
|
|
|
|
}
|
2013-07-27 07:05:26 +00:00
|
|
|
// check and get attributes
|
|
|
|
$_attr = $this->getAttributes($compiler, $args);
|
2015-09-01 01:54:28 +02:00
|
|
|
$compiler->blockTagNestingLevel ++;
|
|
|
|
$this->openTag($compiler, 'block', array($_attr, $compiler->nocache, $compiler->parser->current_buffer,
|
|
|
|
$compiler->template->compiled->has_nocache_code,
|
|
|
|
$compiler->template->caching));
|
|
|
|
// must whole block be nocache ?
|
|
|
|
if ($compiler->tag_nocache) {
|
|
|
|
$i = 0;
|
2014-10-01 21:03:40 +00:00
|
|
|
}
|
2013-08-24 18:46:31 +00:00
|
|
|
$compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
|
2015-09-01 01:54:28 +02:00
|
|
|
// $compiler->suppressNocacheProcessing = true;
|
|
|
|
if ($_attr['nocache'] === true) {
|
|
|
|
//$compiler->trigger_template_error('nocache option not allowed', $compiler->parser->lex->taglineno);
|
|
|
|
}
|
2015-08-06 01:19:11 +02:00
|
|
|
$compiler->parser->current_buffer = new Smarty_Internal_ParseTree_Template();
|
2015-09-01 01:54:28 +02:00
|
|
|
$compiler->template->compiled->has_nocache_code = false;
|
|
|
|
$compiler->suppressNocacheProcessing = true;
|
2013-07-27 07:05:26 +00:00
|
|
|
}
|
2010-11-11 21:34:36 +00:00
|
|
|
|
2013-07-27 07:05:26 +00:00
|
|
|
/**
|
|
|
|
* Compile saved child block source
|
|
|
|
*
|
2015-09-01 01:54:28 +02:00
|
|
|
* @param \Smarty_Internal_TemplateCompilerBase compiler object
|
|
|
|
* @param string $_name optional name of child block
|
2014-06-06 02:40:04 +00:00
|
|
|
*
|
2015-09-01 01:54:28 +02:00
|
|
|
* @return string compiled code of child block
|
2013-07-27 07:05:26 +00:00
|
|
|
*/
|
2015-07-01 03:25:55 +02:00
|
|
|
static function compileChildBlock(Smarty_Internal_TemplateCompilerBase $compiler, $_name = null)
|
2013-07-27 07:05:26 +00:00
|
|
|
{
|
2015-09-01 01:54:28 +02:00
|
|
|
if (!$compiler->blockTagNestingLevel) {
|
2015-09-14 23:46:17 +02:00
|
|
|
$compiler->trigger_template_error(' tag {$smarty.block.child} used outside {block} tags ',
|
|
|
|
$compiler->parser->lex->taglineno);
|
2013-07-27 07:05:26 +00:00
|
|
|
}
|
2013-08-24 18:46:31 +00:00
|
|
|
$compiler->has_code = true;
|
2015-09-01 01:54:28 +02:00
|
|
|
$compiler->suppressNocacheProcessing = true;
|
|
|
|
$compiler->callChildBlock[$compiler->blockTagNestingLevel] = true;
|
|
|
|
$_output = "<?php \n\$_smarty_tpl->_Block->callChildBlock(\$_smarty_tpl, \$block);?>";
|
2013-07-27 07:05:26 +00:00
|
|
|
return $_output;
|
2011-12-20 17:50:15 +00:00
|
|
|
}
|
2010-11-11 21:34:36 +00:00
|
|
|
|
2013-08-24 18:46:31 +00:00
|
|
|
/**
|
|
|
|
* Compile $smarty.block.parent
|
|
|
|
*
|
2015-07-01 03:25:55 +02:00
|
|
|
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
|
|
|
|
* @param string $_name optional name of child block
|
2014-06-06 02:40:04 +00:00
|
|
|
*
|
2015-09-01 01:54:28 +02:00
|
|
|
* @return string compiled code of child block
|
2013-08-24 18:46:31 +00:00
|
|
|
*/
|
2015-07-01 03:25:55 +02:00
|
|
|
static function compileParentBlock(Smarty_Internal_TemplateCompilerBase $compiler, $_name = null)
|
2013-08-24 18:46:31 +00:00
|
|
|
{
|
2015-09-14 23:46:17 +02:00
|
|
|
if (!$compiler->inheritanceChild) {
|
|
|
|
$compiler->trigger_template_error(' tag {$smarty.block.parent} used in parent template ',
|
|
|
|
$compiler->parser->lex->taglineno);
|
2013-08-24 18:46:31 +00:00
|
|
|
}
|
2015-09-01 01:54:28 +02:00
|
|
|
if (!$compiler->blockTagNestingLevel) {
|
2015-09-14 23:46:17 +02:00
|
|
|
$compiler->trigger_template_error(' tag {$smarty.block.parent} used outside {block} tags ',
|
|
|
|
$compiler->parser->lex->taglineno);
|
2013-08-24 18:46:31 +00:00
|
|
|
}
|
2015-09-01 01:54:28 +02:00
|
|
|
$compiler->suppressNocacheProcessing = true;
|
|
|
|
$compiler->has_code = true;
|
|
|
|
$_output = "<?php \n\$_smarty_tpl->_Block->callParentBlock(\$_smarty_tpl, \$block);?>";
|
|
|
|
return $_output;
|
2013-08-24 18:46:31 +00:00
|
|
|
}
|
2013-07-27 07:05:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Smarty Internal Plugin Compile BlockClose Class
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class Smarty_Internal_Compile_Blockclose extends Smarty_Internal_CompileBase
|
|
|
|
{
|
2011-12-20 17:50:15 +00:00
|
|
|
/**
|
2013-07-27 07:05:26 +00:00
|
|
|
* Compiles code for the {/block} tag
|
|
|
|
*
|
2015-09-01 01:54:28 +02:00
|
|
|
* @param array $args array with attributes from parser
|
|
|
|
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
|
|
|
|
* @param array $parameter array with compilation parameter
|
2014-06-06 02:40:04 +00:00
|
|
|
*
|
2015-09-01 01:54:28 +02:00
|
|
|
* @return bool true
|
2013-07-27 07:05:26 +00:00
|
|
|
*/
|
2015-09-01 01:54:28 +02:00
|
|
|
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
|
2013-07-14 22:15:45 +00:00
|
|
|
{
|
2015-09-01 01:54:28 +02:00
|
|
|
$this->compiler = $compiler;
|
|
|
|
list($_attr, $_nocache, $_buffer, $_has_nocache_code, $_caching) = $this->closeTag($compiler, array('block'));
|
|
|
|
$_name = trim($_attr['name'], "'\"");
|
|
|
|
$_functionCode = $compiler->parser->current_buffer;
|
|
|
|
// setup buffer for template function code
|
|
|
|
$compiler->parser->current_buffer = new Smarty_Internal_ParseTree_Template();
|
|
|
|
|
2015-09-14 23:46:17 +02:00
|
|
|
$_funcNameCaching =
|
|
|
|
$_funcName = preg_replace('![^\w]+!', '_', "block_function_{$_name}_" . uniqid(rand(), true));
|
2015-09-01 01:54:28 +02:00
|
|
|
if ($compiler->template->compiled->has_nocache_code) {
|
|
|
|
// $compiler->parent_compiler->template->tpl_function[$_name]['call_name_caching'] = $_funcNameCaching;
|
|
|
|
$_funcNameCaching .= '_nocache';
|
2015-09-16 16:23:38 +02:00
|
|
|
$output = "<?php\n\n";
|
|
|
|
$output .= "/* {block '{$_name}'} {$compiler->template->source->type}:{$compiler->template->source->name} */\n";
|
2015-09-01 01:54:28 +02:00
|
|
|
$output .= "function {$_funcNameCaching} (\$_smarty_tpl, \$block) {\n";
|
|
|
|
$output .= "/*/%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/\n";
|
|
|
|
$output .= "\$_smarty_tpl->cached->hashes['{$compiler->template->compiled->nocache_hash}'] = true;\n?>\n";
|
2015-09-14 23:46:17 +02:00
|
|
|
$compiler->parser->current_buffer->append_subtree($compiler->parser,
|
|
|
|
new Smarty_Internal_ParseTree_Tag($compiler->parser,
|
|
|
|
$output));
|
2015-09-01 01:54:28 +02:00
|
|
|
$compiler->parser->current_buffer->append_subtree($compiler->parser, $_functionCode);
|
|
|
|
$output = "<?php /*%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/\n";
|
2015-09-16 16:23:38 +02:00
|
|
|
$output .= "\n}\n";
|
|
|
|
$output .= "/* {/block '{$_name}'} */\n\n";
|
2015-09-01 01:54:28 +02:00
|
|
|
$output .= "?>\n";
|
2015-09-14 23:46:17 +02:00
|
|
|
$compiler->parser->current_buffer->append_subtree($compiler->parser,
|
|
|
|
new Smarty_Internal_ParseTree_Tag($compiler->parser,
|
|
|
|
$output));
|
2015-09-16 16:23:38 +02:00
|
|
|
$compiler->blockOrFunctionCode .= $f =
|
2015-09-14 23:46:17 +02:00
|
|
|
$compiler->parser->current_buffer->to_smarty_php($compiler->parser);
|
2015-09-01 01:54:28 +02:00
|
|
|
$compiler->parser->current_buffer = new Smarty_Internal_ParseTree_Template();
|
2015-09-14 23:46:17 +02:00
|
|
|
$_functionCode = new Smarty_Internal_ParseTree_Tag($compiler->parser,
|
|
|
|
preg_replace_callback("/((<\?php )?echo '\/\*%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%\*\/([\S\s]*?)\/\*\/%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%\*\/';(\?>\n)?)/",
|
|
|
|
array($this, 'removeNocache'),
|
|
|
|
$_functionCode->to_smarty_php($compiler->parser)));
|
2015-09-01 01:54:28 +02:00
|
|
|
}
|
2015-09-16 16:23:38 +02:00
|
|
|
$output = "<?php\n\n";
|
|
|
|
$output .= "/* {block '{$_name}'} {$compiler->template->source->type}:{$compiler->template->source->name} */\n";
|
2015-09-01 01:54:28 +02:00
|
|
|
$output .= "function {$_funcName}(\$_smarty_tpl, \$block) {?>";
|
2015-09-14 23:46:17 +02:00
|
|
|
$compiler->parser->current_buffer->append_subtree($compiler->parser,
|
|
|
|
new Smarty_Internal_ParseTree_Tag($compiler->parser,
|
|
|
|
$output));
|
2015-09-01 01:54:28 +02:00
|
|
|
$compiler->parser->current_buffer->append_subtree($compiler->parser, $_functionCode);
|
2015-09-16 16:23:38 +02:00
|
|
|
$output = "<?php\n}\n";
|
|
|
|
$output .= "/* {/block '{$_name}'} */\n\n";
|
2015-09-01 01:54:28 +02:00
|
|
|
$output .= "?>\n";
|
2015-09-14 23:46:17 +02:00
|
|
|
$compiler->parser->current_buffer->append_subtree($compiler->parser,
|
|
|
|
new Smarty_Internal_ParseTree_Tag($compiler->parser,
|
|
|
|
$output));
|
2015-09-16 16:23:38 +02:00
|
|
|
$compiler->blockOrFunctionCode .= $f =
|
2015-09-14 23:46:17 +02:00
|
|
|
$compiler->parser->current_buffer->to_smarty_php($compiler->parser);
|
2015-09-01 01:54:28 +02:00
|
|
|
// nocache plugins must be copied
|
|
|
|
if (!empty($compiler->template->compiled->required_plugins['nocache'])) {
|
|
|
|
foreach ($compiler->template->compiled->required_plugins['nocache'] as $plugin => $tmp) {
|
|
|
|
foreach ($tmp as $type => $data) {
|
2015-09-14 23:46:17 +02:00
|
|
|
$compiler->parent_compiler->template->compiled->required_plugins['compiled'][$plugin][$type] =
|
|
|
|
$data;
|
2013-08-24 18:46:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-01 01:54:28 +02:00
|
|
|
// restore old status
|
|
|
|
$compiler->template->compiled->has_nocache_code = $_has_nocache_code;
|
|
|
|
$compiler->tag_nocache = $compiler->nocache;
|
|
|
|
$compiler->nocache = $_nocache;
|
|
|
|
$compiler->parser->current_buffer = $_buffer;
|
|
|
|
|
|
|
|
$_parameter = $_attr;
|
|
|
|
foreach ($_parameter as $name => $stat) {
|
|
|
|
if ($stat === false) {
|
|
|
|
unset($_parameter[$name]);
|
2011-12-20 17:50:15 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-01 01:54:28 +02:00
|
|
|
if (isset($compiler->callChildBlock[$compiler->blockTagNestingLevel])) {
|
|
|
|
$_parameter['callChildBlock'] = 'true';
|
|
|
|
unset($compiler->callChildBlock[$compiler->blockTagNestingLevel]);
|
2015-06-04 02:39:26 +02:00
|
|
|
}
|
2015-09-01 01:54:28 +02:00
|
|
|
$compiler->blockTagNestingLevel --;
|
2015-09-14 23:46:17 +02:00
|
|
|
// inner {block} or child template {block} must register block
|
|
|
|
if ($compiler->blockTagNestingLevel == 0 && $compiler->inheritanceChild) {
|
2015-09-01 01:54:28 +02:00
|
|
|
$_function = 'register';
|
|
|
|
} else {
|
|
|
|
$_function = 'call';
|
2013-08-24 18:46:31 +00:00
|
|
|
}
|
2015-09-01 01:54:28 +02:00
|
|
|
$cm = $compiler->template->caching ? 'true' : 'false';
|
2015-09-14 23:46:17 +02:00
|
|
|
$output =
|
|
|
|
"<?php \n\$_smarty_tpl->_Block->{$_function}Block(\$_smarty_tpl, array('caching' => {$cm}, 'function' => '{$_funcNameCaching}'";
|
2015-09-01 01:54:28 +02:00
|
|
|
foreach ($_parameter as $name => $stat) {
|
|
|
|
if ($stat !== false) {
|
|
|
|
$output .= ", '{$name}' => {$stat}";
|
|
|
|
}
|
2013-08-24 18:46:31 +00:00
|
|
|
}
|
2015-09-01 01:54:28 +02:00
|
|
|
$output .= "));\n?>\n";
|
|
|
|
$compiler->has_code = true;
|
|
|
|
$compiler->suppressNocacheProcessing = true;
|
|
|
|
return $output;
|
2013-08-24 18:46:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-09-01 01:54:28 +02:00
|
|
|
* @param $match
|
2013-08-24 18:46:31 +00:00
|
|
|
*
|
2015-09-01 01:54:28 +02:00
|
|
|
* @return mixed
|
2013-08-24 18:46:31 +00:00
|
|
|
*/
|
2015-09-01 01:54:28 +02:00
|
|
|
function removeNocache($match)
|
2013-08-24 18:46:31 +00:00
|
|
|
{
|
2015-09-14 23:46:17 +02:00
|
|
|
$code =
|
|
|
|
preg_replace("/((<\?php )?echo '\/\*%%SmartyNocache:{$this->compiler->template->compiled->nocache_hash}%%\*\/)|(\/\*\/%%SmartyNocache:{$this->compiler->template->compiled->nocache_hash}%%\*\/';(\?>\n)?)/",
|
|
|
|
'', $match[0]);
|
2015-09-01 01:54:28 +02:00
|
|
|
$code = str_replace(array('\\\'', '\\\\\''), array('\'', '\\\''), $code);
|
|
|
|
return $code;
|
2013-08-24 18:46:31 +00:00
|
|
|
}
|
2013-07-27 07:05:26 +00:00
|
|
|
}
|