Skip to content

Commit

Permalink
- compile {block} tag code and its processing into classes
Browse files Browse the repository at this point in the history
  • Loading branch information
uwetews committed Dec 25, 2015
1 parent 2ebacc3 commit fdd20dd
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 207 deletions.
3 changes: 3 additions & 0 deletions change_log.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
 ===== 3.1.30-dev ===== (xx.xx.xx)
25.12.2015
- compile {block} tag code and its processing into classes

24.12.2015
- new feature Compiler does now observe the template_dir setting and will create separate compiled files if required
- bugfix post filter did fail on template inheritance https://github.com/smarty-php/smarty/issues/144
Expand Down
2 changes: 1 addition & 1 deletion libs/Smarty.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class Smarty extends Smarty_Internal_TemplateBase
/**
* smarty version
*/
const SMARTY_VERSION = '3.1.30-dev/8';
const SMARTY_VERSION = '3.1.30-dev/9';

/**
* define variable scopes
Expand Down
155 changes: 155 additions & 0 deletions libs/sysplugins/smarty_internal_block.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php

/**
* Smarty {block} tag class
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Block
{
/**
* Block name
*
* @var string
*/
public $name = '';

/**
* Hide attribute
*
* @var bool
*/
public $hide = false;

/**
* Append attribute
*
* @var bool
*/
public $append = false;

/**
* prepend attribute
*
* @var bool
*/
public $prepend = false;

/**
* Block calls {$smarty.block.child}
*
* @var bool
*/
public $callsChild = false;

/**
* Inheritance child block
*
* @var Smarty_Internal_Block|null
*/
public $child = null;

/**
* Inheritance calling parent block
*
* @var Smarty_Internal_Block|null
*/
public $parent = null;

/**
* Inheritance Template index
*
* @var int
*/
public $tplIndex = 0;

/**
* Smarty_Internal_Block constructor.
*
* @param \Smarty_Internal_Template $_smarty_tpl
*
*/
public function __construct(Smarty_Internal_Template $_smarty_tpl)
{
$this->tplIndex = $_smarty_tpl->ext->_inheritance->tplIndex;
if (isset($_smarty_tpl->ext->_inheritance->blockParameter[ $this->name ])) {
$this->child = $_smarty_tpl->ext->_inheritance->blockParameter[ $this->name ];
}
if ($_smarty_tpl->ext->_inheritance->state == 1) {
$_smarty_tpl->ext->_inheritance->blockParameter[ $this->name ] = $this;
return;
}
$this->process($_smarty_tpl);
}

/**
* Goto child block or render this
*
* @param \Smarty_Internal_Template $_smarty_tpl
* @param \Smarty_Internal_Block|null $parent
*/
public function process(Smarty_Internal_Template $_smarty_tpl, Smarty_Internal_Block $parent = null) {
if ($this->hide && !isset($this->child)) {
return;
}
if (isset($this->child) && $this->child->hide && !isset($this->child->child)) {
$this->child = null;
}
$this->parent = $parent;
if ($this->append && !$this->prepend && isset($parent)) {
$this->callParent($_smarty_tpl);
}
if ($this->callsChild || !isset($this->child) || ($this->child->hide && !isset($this->child->child))) {
$this->callBlock($_smarty_tpl);
} else {
$this->child->process($_smarty_tpl, $this);
}
if ($this->prepend && isset($parent)) {
$this->callParent($_smarty_tpl);
if ($this->append) {
if ($this->callsChild || !isset($this->child) || ($this->child->hide && !isset($this->child->child))) {
$this->callBlock($_smarty_tpl);
} else {
$this->child->process($_smarty_tpl, $this);
}
}
}
$this->parent = null;
}

/**
* Compiled block code overloaded by {block} class
*
* @param \Smarty_Internal_Template $_smarty_tpl
*/
public function callBlock(Smarty_Internal_Template $_smarty_tpl) {
}

/**
* Render child on {$smarty.block.child}
*
* @param \Smarty_Internal_Template $_smarty_tpl
*/
public function callChild (Smarty_Internal_Template $_smarty_tpl) {
if (isset($this->child)) {
$this->child->process($_smarty_tpl, $this);
}
}

/**
* Render parent on {$smarty.block.parent} or {block append/prepend} *
*
* @param \Smarty_Internal_Template $_smarty_tpl
*
* @throws \SmartyException
*/
public function callParent (Smarty_Internal_Template $_smarty_tpl) {
if (isset($this->parent)) {
$this->parent->callBlock($_smarty_tpl, $this->parent->parent);
} else {
throw new SmartyException("inheritance: illegal {\$smarty.block.parent} or {block append/prepend} used in parent template '{$_smarty_tpl->ext->_inheritance->compiledFilePath[$this->tplIndex]}' block '{$this->name}'");
}
}
}
127 changes: 42 additions & 85 deletions libs/sysplugins/smarty_internal_compile_block.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ class Smarty_Internal_Compile_Block extends Smarty_Internal_Compile_Shared_Inher
*/
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
{
if (!isset($compiler->_cache['blockNesting'])) {
$compiler->_cache['blockNesting'] = 0;
if (!isset($compiler->_cache[ 'blockNesting' ])) {
$compiler->_cache[ 'blockNesting' ] = 0;
}
if ($compiler->_cache['blockNesting'] == 0) {
if ($compiler->_cache[ 'blockNesting' ] == 0) {
// make sure that inheritance gets initialized in template code
$this->registerInit($compiler);
$this->option_flags = array('hide', 'nocache', 'append', 'prepend');
Expand All @@ -84,10 +84,9 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $
}
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
$compiler->_cache['blockNesting'] ++;
$compiler->_cache['blockName'][$compiler->_cache['blockNesting']] = $_attr['name'];
$compiler->_cache['blockParams'][$compiler->_cache['blockNesting']][0] = 'block_' . preg_replace('![^\w]+!', '_', uniqid(rand(), true));
$compiler->_cache['blockParams'][$compiler->_cache['blockNesting']][1] = false;
$compiler->_cache[ 'blockNesting' ] ++;
$compiler->_cache[ 'blockName' ][ $compiler->_cache[ 'blockNesting' ] ] = $_attr[ 'name' ];
$compiler->_cache[ 'blockParams' ][ $compiler->_cache[ 'blockNesting' ] ][ 'name' ] = "{$_attr['name']}";
$this->openTag($compiler, 'block', array($_attr, $compiler->nocache, $compiler->parser->current_buffer,
$compiler->template->compiled->has_nocache_code,
$compiler->template->caching));
Expand All @@ -97,7 +96,7 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $
}
$compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
// $compiler->suppressNocacheProcessing = true;
if ($_attr['nocache'] === true) {
if ($_attr[ 'nocache' ] === true) {
//$compiler->trigger_template_error('nocache option not allowed', $compiler->parser->lex->taglineno);
}
$compiler->parser->current_buffer = new Smarty_Internal_ParseTree_Template();
Expand All @@ -115,14 +114,14 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $
*/
static function compileChildBlock(Smarty_Internal_TemplateCompilerBase $compiler, $_name = null)
{
if (!isset($compiler->_cache['blockNesting'])) {
if (!isset($compiler->_cache[ 'blockNesting' ])) {
$compiler->trigger_template_error(' tag {$smarty.block.child} used outside {block} tags ',
$compiler->parser->lex->taglineno);
}
$compiler->has_code = true;
$compiler->suppressNocacheProcessing = true;
$compiler->_cache['blockParams'][$compiler->_cache['blockNesting']][1] = true;
$output = "<?php \n\$_smarty_tpl->ext->_inheritance->processBlock(\$_smarty_tpl, 2, {$compiler->_cache['blockName'][$compiler->_cache['blockNesting']]}, null, \$_blockParentStack);\n?>\n";
$compiler->_cache[ 'blockParams' ][ $compiler->_cache[ 'blockNesting' ] ][ 'callsChild' ] = 'true';
$output = "<?php \n\$this->callChild(\$_smarty_tpl);\n?>\n";
return $output;
}

Expand All @@ -136,13 +135,13 @@ static function compileChildBlock(Smarty_Internal_TemplateCompilerBase $compiler
*/
static function compileParentBlock(Smarty_Internal_TemplateCompilerBase $compiler, $_name = null)
{
if (!isset($compiler->_cache['blockNesting'])) {
if (!isset($compiler->_cache[ 'blockNesting' ])) {
$compiler->trigger_template_error(' tag {$smarty.block.parent} used outside {block} tags ',
$compiler->parser->lex->taglineno);
}
$compiler->suppressNocacheProcessing = true;
$compiler->has_code = true;
$output = "<?php \n\$_smarty_tpl->ext->_inheritance->processBlock(\$_smarty_tpl, 4, {$compiler->_cache['blockName'][$compiler->_cache['blockNesting']]}, null, \$_blockParentStack);\n?>\n";
$output = "<?php \n\$this->callParent(\$_smarty_tpl);\n?>\n";
return $output;
}
}
Expand All @@ -166,62 +165,35 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $
{
list($_attr, $_nocache, $_buffer, $_has_nocache_code, $_caching) = $this->closeTag($compiler, array('block'));
// init block parameter
$_block = $compiler->_cache['blockParams'][$compiler->_cache['blockNesting']];
unset($compiler->_cache['blockParams'][$compiler->_cache['blockNesting']]);
$_block[2] = $_block[3] = 0;
$_name = trim($_attr['name'], "'\"");
$_assign = isset($_attr['assign']) ? $_attr['assign'] : null;
unset($_attr['assign'], $_attr['name']);
$_block = $compiler->_cache[ 'blockParams' ][ $compiler->_cache[ 'blockNesting' ] ];
unset($compiler->_cache[ 'blockParams' ][ $compiler->_cache[ 'blockNesting' ] ]);
$_name = trim($_attr[ 'name' ], "'\"");
$_assign = isset($_attr[ 'assign' ]) ? $_attr[ 'assign' ] : null;
unset($_attr[ 'assign' ], $_attr[ 'name' ]);
foreach ($_attr as $name => $stat) {
if ((is_bool($stat) && $stat !== false) || (!is_bool($stat) && $stat != 'false')) {
$_block[$name] = is_string($stat) ? trim($stat, "'\"") : $stat;
$_block[ $name ] = 'true';
}
}
$_funcName = $_block[0];
$_className = 'Block_' . preg_replace('#[^\w\|]+#S', '_', $_name) . '_' .
preg_replace('![^\w]+!', '_', uniqid(rand(), true));
// get compiled block code
$_functionCode = $compiler->parser->current_buffer;
// setup buffer for template function code
$compiler->parser->current_buffer = new Smarty_Internal_ParseTree_Template();

$output = "<?php\n";
$output .= "/* {block '{$_name}'} {$compiler->template->source->type}:{$compiler->template->source->name} */\n";

This comment has been minimized.

Copy link
@talisto

talisto Jan 7, 2016

For a string resource type, the $compiler->template->source->name contains the entire template contents, which can lead to parse errors and other problems. I've submitted an issue, see #157.

$output .= "class {$_className} extends Smarty_Internal_Block\n";
$output .= "{\n";
foreach ($_block as $property => $value) {
$output .= "public \${$property} = {$value};\n";
}
$output .= "public function callBlock(Smarty_Internal_Template \$_smarty_tpl) {\n";
//$output .= "/*/%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/\n";
if ($compiler->template->compiled->has_nocache_code) {
// $compiler->parent_compiler->template->tpl_function[$_name]['call_name_caching'] = $_funcNameCaching;
$_block[6] = $_funcNameCaching = $_funcName . '_nocache';
$output = "<?php\n";
$output .= "/* {block '{$_name}'} {$compiler->template->source->type}:{$compiler->template->source->name} */\n";
$output .= "function {$_funcNameCaching} (\$_smarty_tpl, \$_blockParentStack) {\n";
$output .= "/*/%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/\n";
$output .= "\$_smarty_tpl->cached->hashes['{$compiler->template->compiled->nocache_hash}'] = true;\n";
if (isset($_assign)) {
$output .= "ob_start();\n";
}
$output .= "?>\n";
$compiler->parser->current_buffer->append_subtree($compiler->parser,
new Smarty_Internal_ParseTree_Tag($compiler->parser,
$output));
$compiler->parser->current_buffer->append_subtree($compiler->parser, $_functionCode);
$output = "<?php\n";
if (isset($_assign)) {
$output .= "\$_smarty_tpl->tpl_vars[{$_assign}] = new Smarty_Variable(ob_get_clean());\n";
}
$output .= "/*%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/\n";
$output .= "}\n";
$output .= "/* {/block '{$_name}'} */\n\n";
$output .= "?>\n";
$compiler->parser->current_buffer->append_subtree($compiler->parser,
new Smarty_Internal_ParseTree_Tag($compiler->parser,
$output));
$compiler->blockOrFunctionCode .= $f = $compiler->parser->current_buffer->to_smarty_php($compiler->parser);
$compiler->parser->current_buffer = new Smarty_Internal_ParseTree_Template();
$this->compiler = $compiler;
$_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)));
$this->compiler = null;
}
$output = "<?php\n";
$output .= "/* {block '{$_name}'} {$compiler->template->source->type}:{$compiler->template->source->name} */\n";
$output .= "function {$_funcName}(\$_smarty_tpl, \$_blockParentStack) {\n";
if (isset($_assign)) {
$output .= "ob_start();\n";
}
Expand All @@ -234,59 +206,44 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $
if (isset($_assign)) {
$output .= "\$_smarty_tpl->tpl_vars[{$_assign}] = new Smarty_Variable(ob_get_clean());\n";
}
//$output .= "/*%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/\n";
$output .= "}\n";
$output .= "}\n";
$output .= "/* {/block '{$_name}'} */\n\n";
$output .= "?>\n";
$compiler->parser->current_buffer->append_subtree($compiler->parser,
new Smarty_Internal_ParseTree_Tag($compiler->parser,
$output));
$compiler->blockOrFunctionCode .= $compiler->parser->current_buffer->to_smarty_php($compiler->parser);
$compiler->blockOrFunctionCode .= $f = $compiler->parser->current_buffer->to_smarty_php($compiler->parser);
$compiler->parser->current_buffer = new Smarty_Internal_ParseTree_Template();
// nocache plugins must be copied
if (!empty($compiler->template->compiled->required_plugins['nocache'])) {
foreach ($compiler->template->compiled->required_plugins['nocache'] as $plugin => $tmp) {
if (!empty($compiler->template->compiled->required_plugins[ 'nocache' ])) {
foreach ($compiler->template->compiled->required_plugins[ 'nocache' ] as $plugin => $tmp) {
foreach ($tmp as $type => $data) {
$compiler->parent_compiler->template->compiled->required_plugins['compiled'][$plugin][$type] =
$compiler->parent_compiler->template->compiled->required_plugins[ 'compiled' ][ $plugin ][ $type ] =
$data;
}
}
}


// 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;
$output = "<?php \n";
if ($compiler->_cache['blockNesting'] == 1) {
$output .= "\$_smarty_tpl->ext->_inheritance->processBlock(\$_smarty_tpl, 0, {$compiler->_cache['blockName'][$compiler->_cache['blockNesting']]}, " .
var_export($_block, true) . ");\n";
if ($compiler->_cache[ 'blockNesting' ] == 1) {
$output .= "new {$_className}(\$_smarty_tpl);\n";
} else {
$output .= "\$_smarty_tpl->ext->_inheritance->processBlock(\$_smarty_tpl, 0, {$compiler->_cache['blockName'][$compiler->_cache['blockNesting']]}, " .
var_export($_block, true) . ", \$_blockParentStack);\n";

$output .= "new {$_className}(\$_smarty_tpl);\n";
}
$output .= "?>\n";
$compiler->_cache['blockNesting'] --;
if ($compiler->_cache['blockNesting'] == 0) {
unset($compiler->_cache['blockNesting']);
$compiler->_cache[ 'blockNesting' ] --;
if ($compiler->_cache[ 'blockNesting' ] == 0) {
unset($compiler->_cache[ 'blockNesting' ]);
}
$compiler->has_code = true;
$compiler->suppressNocacheProcessing = true;
return $output;
}

/**
* @param $match
*
* @return mixed
*/
function removeNocache($match)
{
$code =
preg_replace("/((<\?php )?echo '\/\*%%SmartyNocache:{$this->compiler->template->compiled->nocache_hash}%%\*\/)|(\/\*\/%%SmartyNocache:{$this->compiler->template->compiled->nocache_hash}%%\*\/';(\?>\n)?)/",
'', $match[0]);
$code = str_replace(array('\\\'', '\\\\\''), array('\'', '\\\''), $code);
return $code;
}
}
Loading

0 comments on commit fdd20dd

Please sign in to comment.