diff --git a/src/Compile/Base.php b/src/Compile/Base.php index 85bcf72e..2069b9e5 100644 --- a/src/Compile/Base.php +++ b/src/Compile/Base.php @@ -217,7 +217,8 @@ abstract class Base implements CompilerInterface { } } // 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; } // wrong nesting of tags diff --git a/src/Compile/SpecialVariableCompiler.php b/src/Compile/SpecialVariableCompiler.php index 9467e084..d138688a 100644 --- a/src/Compile/SpecialVariableCompiler.php +++ b/src/Compile/SpecialVariableCompiler.php @@ -114,9 +114,9 @@ class SpecialVariableCompiler extends Base { } // no break case 'ldelim': - return "\$_smarty_tpl->smarty->left_delimiter"; + return "\$_smarty_tpl->getLeftDelimiter()"; case 'rdelim': - return "\$_smarty_tpl->smarty->right_delimiter"; + return "\$_smarty_tpl->getRightDelimiter()"; default: $compiler->trigger_template_error('$smarty.' . trim($_index[0], "'") . ' is not defined'); break; diff --git a/src/Compile/Tag/ExtendsTag.php b/src/Compile/Tag/ExtendsTag.php index bcd02344..da2bc223 100644 --- a/src/Compile/Tag/ExtendsTag.php +++ b/src/Compile/Tag/ExtendsTag.php @@ -150,7 +150,7 @@ class ExtendsTag extends Inheritance { foreach ($template->source->components as $source) { $resources[] = $source->resource; } - return $template->smarty->left_delimiter . 'extends file=\'extends:' . join('|', $resources) . - '\' extends_resource=true' . $template->smarty->right_delimiter; + return $template->getLeftDelimiter() . 'extends file=\'extends:' . join('|', $resources) . + '\' extends_resource=true' . $template->getRightDelimiter(); } } diff --git a/src/Compile/Tag/Ldelim.php b/src/Compile/Tag/Ldelim.php index 530592da..71b1389c 100644 --- a/src/Compile/Tag/Ldelim.php +++ b/src/Compile/Tag/Ldelim.php @@ -35,6 +35,6 @@ class Ldelim extends Base { if ($_attr['nocache'] === true) { $compiler->trigger_template_error('nocache option not allowed', null, true); } - return $compiler->smarty->left_delimiter; + return $compiler->template->getLeftDelimiter(); } } diff --git a/src/Compile/Tag/Rdelim.php b/src/Compile/Tag/Rdelim.php index 267a4a96..173d1231 100644 --- a/src/Compile/Tag/Rdelim.php +++ b/src/Compile/Tag/Rdelim.php @@ -30,6 +30,6 @@ class Rdelim extends Ldelim { */ public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) { parent::compile($args, $compiler); - return $compiler->smarty->right_delimiter; + return $compiler->template->getRightDelimiter(); } } diff --git a/src/Compiler/Template.php b/src/Compiler/Template.php index 72157cb1..964ca812 100644 --- a/src/Compiler/Template.php +++ b/src/Compiler/Template.php @@ -1317,8 +1317,8 @@ class Template extends BaseCompiler { // get stacked info [$openTag, $_data] = array_pop($this->_tag_stack); $this->trigger_template_error( - "unclosed {$this->smarty->left_delimiter}" . $openTag . - "{$this->smarty->right_delimiter} tag" + "unclosed " . $this->smarty->getLeftDelimiter() . $openTag . + $this->smarty->getRightDelimiter() . " tag" ); } // call post compile callbacks diff --git a/src/Debug.php b/src/Debug.php index 2bba87ec..43d4e107 100644 --- a/src/Debug.php +++ b/src/Debug.php @@ -205,8 +205,6 @@ class Debug extends Data $debObj->setCompileDir($smarty->getCompileDir()); $debObj->force_compile = false; $debObj->compile_check = \Smarty::COMPILECHECK_ON; - $debObj->left_delimiter = '{'; - $debObj->right_delimiter = '}'; $debObj->security_policy = null; $debObj->debugging = false; $debObj->debugging_ctrl = 'NONE'; diff --git a/src/Smarty.php b/src/Smarty.php index 77efef42..d5554ae1 100644 --- a/src/Smarty.php +++ b/src/Smarty.php @@ -236,14 +236,14 @@ class Smarty extends \Smarty\TemplateBase * * @var string */ - public $left_delimiter = "{"; + private $left_delimiter = "{"; /** * template right-delimiter * * @var string */ - public $right_delimiter = "}"; + private $right_delimiter = "}"; /** * array of strings which shall be treated as literal by compiler diff --git a/src/Template.php b/src/Template.php index 9d9909fa..ae806889 100644 --- a/src/Template.php +++ b/src/Template.php @@ -106,6 +106,20 @@ class Template extends TemplateBase { */ 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 * Some of the global Smarty settings copied to template scope @@ -851,4 +865,45 @@ class Template extends TemplateBase { private function getFrameCompiler(): Compiler\CodeFrame { 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; + } + } diff --git a/src/TemplateBase.php b/src/TemplateBase.php index 1e6bad8b..10fde43b 100644 --- a/src/TemplateBase.php +++ b/src/TemplateBase.php @@ -462,8 +462,8 @@ abstract class TemplateBase extends Data { */ private function _setLiterals(Smarty $smarty, $literals) { $literals = array_combine($literals, $literals); - $error = isset($literals[$smarty->left_delimiter]) ? [$smarty->left_delimiter] : []; - $error = isset($literals[$smarty->right_delimiter]) ? $error[] = $smarty->right_delimiter : $error; + $error = isset($literals[$smarty->getLeftDelimiter()]) ? [$smarty->getLeftDelimiter()] : []; + $error = isset($literals[$smarty->getRightDelimiter()]) ? $error[] = $smarty->getRightDelimiter() : $error; if (!empty($error)) { throw new Exception( 'User defined literal(s) "' . $error . diff --git a/tests/UnitTests/A_Core/GetterSetter/GetterSetterTest.php b/tests/UnitTests/A_Core/GetterSetter/GetterSetterTest.php index 6a8fab62..43d622b8 100644 --- a/tests/UnitTests/A_Core/GetterSetter/GetterSetterTest.php +++ b/tests/UnitTests/A_Core/GetterSetter/GetterSetterTest.php @@ -32,8 +32,8 @@ class GetterSetterTest extends PHPUnit_Smarty { $this->smarty->setLeftDelimiter('<{'); $this->smarty->setRightDelimiter('}>'); - $this->assertEquals('<{', $this->smarty->left_delimiter); - $this->assertEquals('}>', $this->smarty->right_delimiter); + $this->assertEquals('<{', $this->smarty->getLeftDelimiter()); + $this->assertEquals('}>', $this->smarty->getRightDelimiter()); } /** @@ -55,10 +55,10 @@ class GetterSetterTest extends PHPUnit_Smarty $tpl = $this->smarty->createTemplate('helloworld.tpl'); $tpl->setLeftDelimiter('<{'); $tpl->setRightDelimiter('}>'); - $this->assertEquals('<{', $tpl->smarty->left_delimiter); - $this->assertEquals('}>', $tpl->smarty->right_delimiter); - $this->assertEquals('{', $this->smarty->left_delimiter); - $this->assertEquals('}', $this->smarty->right_delimiter); + $this->assertEquals('<{', $tpl->getLeftDelimiter()); + $this->assertEquals('}>', $tpl->getRightDelimiter()); + $this->assertEquals('{', $this->smarty->getLeftDelimiter()); + $this->assertEquals('}', $this->smarty->getRightDelimiter()); } /** diff --git a/tests/UnitTests/Compiler/Delimiter/DelimiterTest.php b/tests/UnitTests/Compiler/Delimiter/DelimiterTest.php index ceb98e55..4092d9fb 100644 --- a/tests/UnitTests/Compiler/Delimiter/DelimiterTest.php +++ b/tests/UnitTests/Compiler/Delimiter/DelimiterTest.php @@ -30,8 +30,8 @@ class DelimiterTest extends PHPUnit_Smarty */ public function testDelimiter1() { - $this->smarty->left_delimiter = '<{'; - $this->smarty->right_delimiter = '}>'; + $this->smarty->setLeftDelimiter('<{'); + $this->smarty->setRightDelimiter('}>'); $tpl = $this->smarty->createTemplate('eval:start <{* comment *}>hello <{if true}><{"world"}><{/if}> end'); $this->assertEquals("start hello world end", $this->smarty->fetch($tpl)); } @@ -40,8 +40,8 @@ class DelimiterTest extends PHPUnit_Smarty */ public function testDelimiter10() { - $this->smarty->left_delimiter = '<'; - $this->smarty->right_delimiter = '>'; + $this->smarty->setLeftDelimiter('<'); + $this->smarty->setRightDelimiter('>'); $tpl = $this->smarty->createTemplate('eval:start <* comment *>hello <"world"> end'); $this->assertEquals("start hello world end", $this->smarty->fetch($tpl)); } @@ -51,8 +51,8 @@ class DelimiterTest extends PHPUnit_Smarty */ public function testDelimiter2() { - $this->smarty->left_delimiter = '<-{'; - $this->smarty->right_delimiter = '}->'; + $this->smarty->setLeftDelimiter('<-{'); + $this->smarty->setRightDelimiter('}->'); $tpl = $this->smarty->createTemplate('eval:<-<-{* comment *}-><-{if true}-><-{"hello world"}-><-{/if}->->'); $this->assertEquals("<-hello world->", $this->smarty->fetch($tpl)); } @@ -62,8 +62,8 @@ class DelimiterTest extends PHPUnit_Smarty */ public function testDelimiter3() { - $this->smarty->left_delimiter = '<--{'; - $this->smarty->right_delimiter = '}-->'; + $this->smarty->setLeftDelimiter('<--{'); + $this->smarty->setRightDelimiter('}-->'); $tpl = $this->smarty->createTemplate('eval:<--{* comment *}--><--{if true}--><--{"hello world"}--><--{/if}-->'); $this->assertEquals("hello world", $this->smarty->fetch($tpl)); } @@ -73,8 +73,8 @@ class DelimiterTest extends PHPUnit_Smarty */ public function testDelimiter4() { - $this->smarty->left_delimiter = '{{'; - $this->smarty->right_delimiter = '}}'; + $this->smarty->setLeftDelimiter('{{'); + $this->smarty->setRightDelimiter('}}'); $tpl = $this->smarty->createTemplate('eval:{{* comment *}}{{if true}}{{"hello world"}}{{/if}}'); $this->assertEquals("hello world", $this->smarty->fetch($tpl)); } @@ -84,8 +84,8 @@ class DelimiterTest extends PHPUnit_Smarty */ public function testDelimiter5() { - $this->smarty->left_delimiter = '{='; - $this->smarty->right_delimiter = '=}'; + $this->smarty->setLeftDelimiter('{='); + $this->smarty->setRightDelimiter('=}'); $tpl = $this->smarty->createTemplate('eval:{=assign var=foo value="hello world" nocache=}{=$foo=}'); $this->assertEquals("hello world", $this->smarty->fetch($tpl)); } @@ -94,8 +94,8 @@ class DelimiterTest extends PHPUnit_Smarty */ public function testDelimiterIssue450() { - $this->smarty->left_delimiter = '{^'; - $this->smarty->right_delimiter = '^}'; + $this->smarty->setLeftDelimiter('{^'); + $this->smarty->setRightDelimiter('^}'); $tpl = $this->smarty->createTemplate('eval:{^assign var=foo value="hello world" nocache^}{^$foo^}'); $this->assertEquals("hello world", $this->smarty->fetch($tpl)); } diff --git a/tests/UnitTests/TemplateSource/_Issues/419/ExtendsIssue419Test.php b/tests/UnitTests/TemplateSource/_Issues/419/ExtendsIssue419Test.php index d7d59405..2079fc0a 100644 --- a/tests/UnitTests/TemplateSource/_Issues/419/ExtendsIssue419Test.php +++ b/tests/UnitTests/TemplateSource/_Issues/419/ExtendsIssue419Test.php @@ -27,8 +27,8 @@ class ExtendsIssue419Test extends PHPUnit_Smarty public function testextends419() { - $this->smarty->left_delimiter = '{{'; - $this->smarty->right_delimiter = '}}'; + $this->smarty->setLeftDelimiter('{{'); + $this->smarty->setRightDelimiter('}}'); $this->assertEquals('child', $this->smarty->fetch('extends:001_parent.tpl|001_child.tpl')); }