Fixed issue related to scoping of left/right delimiter overrides

This commit is contained in:
Simon Wisselink
2023-01-09 11:25:09 +01:00
parent 34d5d6d024
commit e124f71dd8
13 changed files with 91 additions and 37 deletions

View File

@@ -217,7 +217,8 @@ abstract class Base implements CompilerInterface {
} }
} }
// wrong nesting of tags // wrong nesting of tags
$compiler->trigger_template_error("unclosed '{$compiler->smarty->left_delimiter}{$_openTag}{$compiler->smarty->right_delimiter}' tag"); $compiler->trigger_template_error("unclosed '" . $compiler->template->getLeftDelimiter() . "{$_openTag}" .
$compiler->template->getRightDelimiter() . "' tag");
return; return;
} }
// wrong nesting of tags // wrong nesting of tags

View File

@@ -114,9 +114,9 @@ class SpecialVariableCompiler extends Base {
} }
// no break // no break
case 'ldelim': case 'ldelim':
return "\$_smarty_tpl->smarty->left_delimiter"; return "\$_smarty_tpl->getLeftDelimiter()";
case 'rdelim': case 'rdelim':
return "\$_smarty_tpl->smarty->right_delimiter"; return "\$_smarty_tpl->getRightDelimiter()";
default: default:
$compiler->trigger_template_error('$smarty.' . trim($_index[0], "'") . ' is not defined'); $compiler->trigger_template_error('$smarty.' . trim($_index[0], "'") . ' is not defined');
break; break;

View File

@@ -150,7 +150,7 @@ class ExtendsTag extends Inheritance {
foreach ($template->source->components as $source) { foreach ($template->source->components as $source) {
$resources[] = $source->resource; $resources[] = $source->resource;
} }
return $template->smarty->left_delimiter . 'extends file=\'extends:' . join('|', $resources) . return $template->getLeftDelimiter() . 'extends file=\'extends:' . join('|', $resources) .
'\' extends_resource=true' . $template->smarty->right_delimiter; '\' extends_resource=true' . $template->getRightDelimiter();
} }
} }

View File

@@ -35,6 +35,6 @@ class Ldelim extends Base {
if ($_attr['nocache'] === true) { if ($_attr['nocache'] === true) {
$compiler->trigger_template_error('nocache option not allowed', null, true); $compiler->trigger_template_error('nocache option not allowed', null, true);
} }
return $compiler->smarty->left_delimiter; return $compiler->template->getLeftDelimiter();
} }
} }

View File

@@ -30,6 +30,6 @@ class Rdelim extends Ldelim {
*/ */
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) { public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
parent::compile($args, $compiler); parent::compile($args, $compiler);
return $compiler->smarty->right_delimiter; return $compiler->template->getRightDelimiter();
} }
} }

View File

@@ -1317,8 +1317,8 @@ class Template extends BaseCompiler {
// get stacked info // get stacked info
[$openTag, $_data] = array_pop($this->_tag_stack); [$openTag, $_data] = array_pop($this->_tag_stack);
$this->trigger_template_error( $this->trigger_template_error(
"unclosed {$this->smarty->left_delimiter}" . $openTag . "unclosed " . $this->smarty->getLeftDelimiter() . $openTag .
"{$this->smarty->right_delimiter} tag" $this->smarty->getRightDelimiter() . " tag"
); );
} }
// call post compile callbacks // call post compile callbacks

View File

@@ -205,8 +205,6 @@ class Debug extends Data
$debObj->setCompileDir($smarty->getCompileDir()); $debObj->setCompileDir($smarty->getCompileDir());
$debObj->force_compile = false; $debObj->force_compile = false;
$debObj->compile_check = \Smarty::COMPILECHECK_ON; $debObj->compile_check = \Smarty::COMPILECHECK_ON;
$debObj->left_delimiter = '{';
$debObj->right_delimiter = '}';
$debObj->security_policy = null; $debObj->security_policy = null;
$debObj->debugging = false; $debObj->debugging = false;
$debObj->debugging_ctrl = 'NONE'; $debObj->debugging_ctrl = 'NONE';

View File

@@ -236,14 +236,14 @@ class Smarty extends \Smarty\TemplateBase
* *
* @var string * @var string
*/ */
public $left_delimiter = "{"; private $left_delimiter = "{";
/** /**
* template right-delimiter * template right-delimiter
* *
* @var string * @var string
*/ */
public $right_delimiter = "}"; private $right_delimiter = "}";
/** /**
* array of strings which shall be treated as literal by compiler * array of strings which shall be treated as literal by compiler

View File

@@ -106,6 +106,20 @@ class Template extends TemplateBase {
*/ */
public $endRenderCallbacks = []; public $endRenderCallbacks = [];
/**
* Template left-delimiter. If null, defaults to $this->getSmarty()-getLeftDelimiter().
*
* @var string
*/
private $left_delimiter = null;
/**
* Template right-delimiter. If null, defaults to $this->getSmarty()-getRightDelimiter().
*
* @var string
*/
private $right_delimiter = null;
/** /**
* Create template data object * Create template data object
* Some of the global Smarty settings copied to template scope * Some of the global Smarty settings copied to template scope
@@ -851,4 +865,45 @@ class Template extends TemplateBase {
private function getFrameCompiler(): Compiler\CodeFrame { private function getFrameCompiler(): Compiler\CodeFrame {
return new \Smarty\Compiler\CodeFrame($this); return new \Smarty\Compiler\CodeFrame($this);
} }
/**
* Get left delimiter
*
* @return string
*/
public function getLeftDelimiter()
{
return $this->left_delimiter ?? $this->_getSmartyObj()->getLeftDelimiter();
}
/**
* Set left delimiter
*
* @param string $left_delimiter
*/
public function setLeftDelimiter($left_delimiter)
{
$this->left_delimiter = $left_delimiter;
}
/**
* Get right delimiter
*
* @return string $right_delimiter
*/
public function getRightDelimiter()
{
return $this->right_delimiter ?? $this->_getSmartyObj()->getRightDelimiter();;
}
/**
* Set right delimiter
*
* @param string
*/
public function setRightDelimiter($right_delimiter)
{
$this->right_delimiter = $right_delimiter;
}
} }

View File

@@ -462,8 +462,8 @@ abstract class TemplateBase extends Data {
*/ */
private function _setLiterals(Smarty $smarty, $literals) { private function _setLiterals(Smarty $smarty, $literals) {
$literals = array_combine($literals, $literals); $literals = array_combine($literals, $literals);
$error = isset($literals[$smarty->left_delimiter]) ? [$smarty->left_delimiter] : []; $error = isset($literals[$smarty->getLeftDelimiter()]) ? [$smarty->getLeftDelimiter()] : [];
$error = isset($literals[$smarty->right_delimiter]) ? $error[] = $smarty->right_delimiter : $error; $error = isset($literals[$smarty->getRightDelimiter()]) ? $error[] = $smarty->getRightDelimiter() : $error;
if (!empty($error)) { if (!empty($error)) {
throw new Exception( throw new Exception(
'User defined literal(s) "' . $error . 'User defined literal(s) "' . $error .

View File

@@ -32,8 +32,8 @@ class GetterSetterTest extends PHPUnit_Smarty
{ {
$this->smarty->setLeftDelimiter('<{'); $this->smarty->setLeftDelimiter('<{');
$this->smarty->setRightDelimiter('}>'); $this->smarty->setRightDelimiter('}>');
$this->assertEquals('<{', $this->smarty->left_delimiter); $this->assertEquals('<{', $this->smarty->getLeftDelimiter());
$this->assertEquals('}>', $this->smarty->right_delimiter); $this->assertEquals('}>', $this->smarty->getRightDelimiter());
} }
/** /**
@@ -55,10 +55,10 @@ class GetterSetterTest extends PHPUnit_Smarty
$tpl = $this->smarty->createTemplate('helloworld.tpl'); $tpl = $this->smarty->createTemplate('helloworld.tpl');
$tpl->setLeftDelimiter('<{'); $tpl->setLeftDelimiter('<{');
$tpl->setRightDelimiter('}>'); $tpl->setRightDelimiter('}>');
$this->assertEquals('<{', $tpl->smarty->left_delimiter); $this->assertEquals('<{', $tpl->getLeftDelimiter());
$this->assertEquals('}>', $tpl->smarty->right_delimiter); $this->assertEquals('}>', $tpl->getRightDelimiter());
$this->assertEquals('{', $this->smarty->left_delimiter); $this->assertEquals('{', $this->smarty->getLeftDelimiter());
$this->assertEquals('}', $this->smarty->right_delimiter); $this->assertEquals('}', $this->smarty->getRightDelimiter());
} }
/** /**

View File

@@ -30,8 +30,8 @@ class DelimiterTest extends PHPUnit_Smarty
*/ */
public function testDelimiter1() public function testDelimiter1()
{ {
$this->smarty->left_delimiter = '<{'; $this->smarty->setLeftDelimiter('<{');
$this->smarty->right_delimiter = '}>'; $this->smarty->setRightDelimiter('}>');
$tpl = $this->smarty->createTemplate('eval:start <{* comment *}>hello <{if true}><{"world"}><{/if}> end'); $tpl = $this->smarty->createTemplate('eval:start <{* comment *}>hello <{if true}><{"world"}><{/if}> end');
$this->assertEquals("start hello world end", $this->smarty->fetch($tpl)); $this->assertEquals("start hello world end", $this->smarty->fetch($tpl));
} }
@@ -40,8 +40,8 @@ class DelimiterTest extends PHPUnit_Smarty
*/ */
public function testDelimiter10() public function testDelimiter10()
{ {
$this->smarty->left_delimiter = '<'; $this->smarty->setLeftDelimiter('<');
$this->smarty->right_delimiter = '>'; $this->smarty->setRightDelimiter('>');
$tpl = $this->smarty->createTemplate('eval:start <* comment *>hello <if 1 < 2><"world"></if> end'); $tpl = $this->smarty->createTemplate('eval:start <* comment *>hello <if 1 < 2><"world"></if> end');
$this->assertEquals("start hello world end", $this->smarty->fetch($tpl)); $this->assertEquals("start hello world end", $this->smarty->fetch($tpl));
} }
@@ -51,8 +51,8 @@ class DelimiterTest extends PHPUnit_Smarty
*/ */
public function testDelimiter2() public function testDelimiter2()
{ {
$this->smarty->left_delimiter = '<-{'; $this->smarty->setLeftDelimiter('<-{');
$this->smarty->right_delimiter = '}->'; $this->smarty->setRightDelimiter('}->');
$tpl = $this->smarty->createTemplate('eval:<-<-{* comment *}-><-{if true}-><-{"hello world"}-><-{/if}->->'); $tpl = $this->smarty->createTemplate('eval:<-<-{* comment *}-><-{if true}-><-{"hello world"}-><-{/if}->->');
$this->assertEquals("<-hello world->", $this->smarty->fetch($tpl)); $this->assertEquals("<-hello world->", $this->smarty->fetch($tpl));
} }
@@ -62,8 +62,8 @@ class DelimiterTest extends PHPUnit_Smarty
*/ */
public function testDelimiter3() public function testDelimiter3()
{ {
$this->smarty->left_delimiter = '<--{'; $this->smarty->setLeftDelimiter('<--{');
$this->smarty->right_delimiter = '}-->'; $this->smarty->setRightDelimiter('}-->');
$tpl = $this->smarty->createTemplate('eval:<--{* comment *}--><--{if true}--><--{"hello world"}--><--{/if}-->'); $tpl = $this->smarty->createTemplate('eval:<--{* comment *}--><--{if true}--><--{"hello world"}--><--{/if}-->');
$this->assertEquals("hello world", $this->smarty->fetch($tpl)); $this->assertEquals("hello world", $this->smarty->fetch($tpl));
} }
@@ -73,8 +73,8 @@ class DelimiterTest extends PHPUnit_Smarty
*/ */
public function testDelimiter4() public function testDelimiter4()
{ {
$this->smarty->left_delimiter = '{{'; $this->smarty->setLeftDelimiter('{{');
$this->smarty->right_delimiter = '}}'; $this->smarty->setRightDelimiter('}}');
$tpl = $this->smarty->createTemplate('eval:{{* comment *}}{{if true}}{{"hello world"}}{{/if}}'); $tpl = $this->smarty->createTemplate('eval:{{* comment *}}{{if true}}{{"hello world"}}{{/if}}');
$this->assertEquals("hello world", $this->smarty->fetch($tpl)); $this->assertEquals("hello world", $this->smarty->fetch($tpl));
} }
@@ -84,8 +84,8 @@ class DelimiterTest extends PHPUnit_Smarty
*/ */
public function testDelimiter5() public function testDelimiter5()
{ {
$this->smarty->left_delimiter = '{='; $this->smarty->setLeftDelimiter('{=');
$this->smarty->right_delimiter = '=}'; $this->smarty->setRightDelimiter('=}');
$tpl = $this->smarty->createTemplate('eval:{=assign var=foo value="hello world" nocache=}{=$foo=}'); $tpl = $this->smarty->createTemplate('eval:{=assign var=foo value="hello world" nocache=}{=$foo=}');
$this->assertEquals("hello world", $this->smarty->fetch($tpl)); $this->assertEquals("hello world", $this->smarty->fetch($tpl));
} }
@@ -94,8 +94,8 @@ class DelimiterTest extends PHPUnit_Smarty
*/ */
public function testDelimiterIssue450() public function testDelimiterIssue450()
{ {
$this->smarty->left_delimiter = '{^'; $this->smarty->setLeftDelimiter('{^');
$this->smarty->right_delimiter = '^}'; $this->smarty->setRightDelimiter('^}');
$tpl = $this->smarty->createTemplate('eval:{^assign var=foo value="hello world" nocache^}{^$foo^}'); $tpl = $this->smarty->createTemplate('eval:{^assign var=foo value="hello world" nocache^}{^$foo^}');
$this->assertEquals("hello world", $this->smarty->fetch($tpl)); $this->assertEquals("hello world", $this->smarty->fetch($tpl));
} }

View File

@@ -27,8 +27,8 @@ class ExtendsIssue419Test extends PHPUnit_Smarty
public function testextends419() public function testextends419()
{ {
$this->smarty->left_delimiter = '{{'; $this->smarty->setLeftDelimiter('{{');
$this->smarty->right_delimiter = '}}'; $this->smarty->setRightDelimiter('}}');
$this->assertEquals('child', $this->smarty->fetch('extends:001_parent.tpl|001_child.tpl')); $this->assertEquals('child', $this->smarty->fetch('extends:001_parent.tpl|001_child.tpl'));
} }