Files
smarty/libs/sysplugins/smarty_internal_compile_private_block_plugin.php

127 lines
5.2 KiB
PHP
Raw Normal View History

<?php
/**
* Smarty Internal Plugin Compile Block Plugin
* Compiles code for the execution of block plugin
2011-09-16 14:19:56 +00:00
*
* @package Smarty
* @subpackage Compiler
* @author Uwe Tews
*/
2010-08-17 15:39:51 +00:00
/**
* Smarty Internal Plugin Compile Block Plugin Class
2011-09-16 14:19:56 +00:00
*
* @package Smarty
2011-09-16 14:19:56 +00:00
* @subpackage Compiler
*/
class Smarty_Internal_Compile_Private_Block_Plugin extends Smarty_Internal_CompileBase
{
2011-09-16 14:19:56 +00:00
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see Smarty_Internal_CompileBase
*/
public $optional_attributes = array('_any');
2016-01-02 11:27:44 +01:00
/**
* nesting level
*
* @var int
*/
public $nesting = 0;
2016-01-02 11:27:44 +01:00
/**
* Compiles code for the execution of block plugin
2011-09-16 14:19:56 +00:00
*
* @param array $args array with attributes from parser
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
* @param array $parameter array with compilation parameter
* @param string $tag name of block plugin
* @param string $function PHP function name
*
* @return string compiled code
*/
2016-01-02 11:27:44 +01:00
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter, $tag, $function = null)
{
2016-01-02 11:27:44 +01:00
if (!isset($tag[ 5 ]) || substr($tag, - 5) != 'close') {
// opening tag of block plugin
2011-09-16 14:19:56 +00:00
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
2016-01-02 11:27:44 +01:00
$this->nesting ++;
if ($_attr[ 'nocache' ] === true) {
2011-09-16 14:19:56 +00:00
$compiler->tag_nocache = true;
}
2016-01-02 11:27:44 +01:00
unset($_attr[ 'nocache' ]);
list($callback, $_paramsArray, $callable) = $this->setup($compiler, $_attr, $tag, $function);
$_params = 'array(' . implode(",", $_paramsArray) . ')';
// compile code
$output = "<?php ";
if (is_array($callback)) {
$output .= "\$_block_plugin{$this->nesting} = isset({$callback[0]}) ? {$callback[0]} : null;\n";
$callback = "\$_block_plugin{$this->nesting}{$callback[1]}";
}
if (isset($callable)) {
$output .= "if (!is_callable({$callable})) {\nthrow new SmartyException('block tag \'{$tag}\' not callable or registered');\n}\n";
}
$output .= "\$_smarty_tpl->smarty->_cache['_tag_stack'][] = array('{$tag}', {$_params});\n";
2016-02-09 01:27:15 +01:00
$output .= "\$_block_repeat{$this->nesting}=true;\necho {$callback}({$_params}, null, \$_smarty_tpl, \$_block_repeat{$this->nesting});\nwhile (\$_block_repeat{$this->nesting}) {\nob_start();\n?>";
2016-01-02 11:27:44 +01:00
$this->openTag($compiler, $tag, array($_params, $compiler->nocache, $callback));
// maybe nocache because of nocache variables or nocache plugin
2011-09-16 14:19:56 +00:00
$compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
} else {
// must endblock be nocache?
2011-09-16 14:19:56 +00:00
if ($compiler->nocache) {
$compiler->tag_nocache = true;
}
// closing tag of block plugin, restore nocache
2016-01-02 11:27:44 +01:00
list($_params, $compiler->nocache, $callback) = $this->closeTag($compiler, substr($tag, 0, - 5));
// This tag does create output
2011-09-16 14:19:56 +00:00
$compiler->has_output = true;
// compile code
2016-01-02 11:27:44 +01:00
if (!isset($parameter[ 'modifier_list' ])) {
$mod_pre = $mod_post = $mod_content = '';
$mod_content2 = 'ob_get_clean()';
} else {
2016-01-02 11:27:44 +01:00
$mod_content2 = "\$_block_content{$this->nesting}";
$mod_content = "\$_block_content{$this->nesting} = ob_get_clean();\n";
$mod_pre = "ob_start();\n";
$mod_post = 'echo ' . $compiler->compileTag('private_modifier', array(),
array('modifierlist' => $parameter[ 'modifier_list' ],
2016-02-09 01:27:15 +01:00
'value' => 'ob_get_clean()')) . ";\n";
}
2016-01-02 11:27:44 +01:00
$output = "<?php " . $mod_content . "\$_block_repeat{$this->nesting}=false;\n" . $mod_pre .
2016-02-09 01:27:15 +01:00
"echo {$callback}({$_params}, " . $mod_content2 .
", \$_smarty_tpl, \$_block_repeat{$this->nesting});\n" . $mod_post . "}\n";
$output .= "array_pop(\$_smarty_tpl->smarty->_cache['_tag_stack']);";
$output .= "?>";
2016-01-02 11:27:44 +01:00
$this->nesting --;
2011-09-16 14:19:56 +00:00
}
return $output . "\n";
2011-09-16 14:19:56 +00:00
}
2016-01-02 11:27:44 +01:00
/**
* Setup callback and parameter array
*
* @param \Smarty_Internal_TemplateCompilerBase $compiler
* @param array $_attr attributes
* @param string $tag
* @param string $function
*
* @return array
*/
public function setup(Smarty_Internal_TemplateCompilerBase $compiler, $_attr, $tag, $function)
{
$_paramsArray = array();
foreach ($_attr as $_key => $_value) {
if (is_int($_key)) {
$_paramsArray[] = "$_key=>$_value";
} else {
$_paramsArray[] = "'$_key'=>$_value";
}
}
return array($function, $_paramsArray, null);
2016-01-02 11:27:44 +01:00
}
2011-09-16 14:19:56 +00:00
}