Files
smarty/libs/sysplugins/smarty_internal_compile_while.php

110 lines
4.4 KiB
PHP
Raw Normal View History

<?php
/**
2011-09-16 14:19:56 +00:00
* Smarty Internal Plugin Compile While
* Compiles the {while} tag
*
* @package Smarty
2011-09-16 14:19:56 +00:00
* @subpackage Compiler
* @author Uwe Tews
2011-09-16 14:19:56 +00:00
*/
2010-08-17 15:39:51 +00:00
/**
2011-09-16 14:19:56 +00:00
* Smarty Internal Plugin Compile While Class
*
* @package Smarty
2011-09-16 14:19:56 +00:00
* @subpackage Compiler
*/
class Smarty_Internal_Compile_While extends Smarty_Internal_CompileBase
{
/**
2011-09-16 14:19:56 +00:00
* Compiles code for the {while} tag
*
2016-02-09 01:27:15 +01:00
* @param array $args array with attributes from parser
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
2016-02-09 01:27:15 +01:00
* @param array $parameter array with compilation parameter
*
2011-09-16 14:19:56 +00:00
* @return string compiled code
* @throws \SmartyCompilerException
2011-09-16 14:19:56 +00:00
*/
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
{
2016-02-09 01:27:15 +01:00
$compiler->loopNesting ++;
// check and get attributes
2011-09-16 14:19:56 +00:00
$_attr = $this->getAttributes($compiler, $args);
$this->openTag($compiler, 'while', $compiler->nocache);
if (!array_key_exists("if condition", $parameter)) {
$compiler->trigger_template_error("missing while condition", null, true);
}
// maybe nocache because of nocache variables
2011-09-16 14:19:56 +00:00
$compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
$_output = "<?php\n";
2016-02-09 01:27:15 +01:00
if (is_array($parameter[ 'if condition' ])) {
2011-09-16 14:19:56 +00:00
if ($compiler->nocache) {
$_nocache = ',true';
// create nocache var to make it know for further compiling
2016-02-09 01:27:15 +01:00
if (is_array($parameter[ 'if condition' ][ 'var' ])) {
$var = trim($parameter[ 'if condition' ][ 'var' ][ 'var' ], "'");
2011-09-16 14:19:56 +00:00
} else {
2016-02-09 01:27:15 +01:00
$var = trim($parameter[ 'if condition' ][ 'var' ], "'");
}
2016-02-09 01:27:15 +01:00
if (isset($compiler->template->tpl_vars[ $var ])) {
$compiler->template->tpl_vars[ $var ]->nocache = true;
} else {
2016-02-09 01:27:15 +01:00
$compiler->template->tpl_vars[ $var ] = new Smarty_Variable(null, true);
2011-09-16 14:19:56 +00:00
}
} else {
$_nocache = '';
}
2016-02-09 01:27:15 +01:00
if (is_array($parameter[ 'if condition' ][ 'var' ])) {
$_output .= "if (!isset(\$_smarty_tpl->tpl_vars[" . $parameter[ 'if condition' ][ 'var' ][ 'var' ] .
"]) || !is_array(\$_smarty_tpl->tpl_vars[" .
$parameter[ 'if condition' ][ 'var' ][ 'var' ] .
"]->value)) \$_smarty_tpl->_createLocalArrayVariable(" .
$parameter[ 'if condition' ][ 'var' ][ 'var' ] . "$_nocache);\n";
$_output .= "while (\$_smarty_tpl->tpl_vars[" . $parameter[ 'if condition' ][ 'var' ][ 'var' ] .
"]->value" . $parameter[ 'if condition' ][ 'var' ][ 'smarty_internal_index' ] . " = " .
$parameter[ 'if condition' ][ 'value' ] . ") {?>";
} else {
2016-02-09 01:27:15 +01:00
$_output .= "if (!isset(\$_smarty_tpl->tpl_vars[" . $parameter[ 'if condition' ][ 'var' ] .
"])) \$_smarty_tpl->tpl_vars[" . $parameter[ 'if condition' ][ 'var' ] .
"] = new Smarty_Variable(null{$_nocache});";
$_output .= "while (\$_smarty_tpl->tpl_vars[" . $parameter[ 'if condition' ][ 'var' ] . "]->value = " .
$parameter[ 'if condition' ][ 'value' ] . ") {?>";
2011-09-16 14:19:56 +00:00
}
} else {
$_output .= "while ({$parameter['if condition']}) {?>";
2016-02-09 01:27:15 +01:00
}
return $_output;
2011-09-16 14:19:56 +00:00
}
}
/**
2011-09-16 14:19:56 +00:00
* Smarty Internal Plugin Compile Whileclose Class
*
* @package Smarty
2011-09-16 14:19:56 +00:00
* @subpackage Compiler
*/
class Smarty_Internal_Compile_Whileclose extends Smarty_Internal_CompileBase
{
/**
2011-09-16 14:19:56 +00:00
* Compiles code for the {/while} tag
*
2016-02-09 01:27:15 +01:00
* @param array $args array with attributes from parser
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
*
2011-09-16 14:19:56 +00:00
* @return string compiled code
*/
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
{
2016-02-09 01:27:15 +01:00
$compiler->loopNesting --;
2010-08-17 15:39:51 +00:00
// must endblock be nocache?
2011-09-16 14:19:56 +00:00
if ($compiler->nocache) {
$compiler->tag_nocache = true;
}
2011-09-16 14:19:56 +00:00
$compiler->nocache = $this->closeTag($compiler, array('while'));
return "<?php }?>\n";
2011-09-16 14:19:56 +00:00
}
}