From 9915ae361034d3c30d7a3e4623675fd00b382114 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 11 Oct 2013 18:53:42 +0100 Subject: [PATCH] CRM-13580 - crmScope - Add support for "dynamic variable scopes" in Smarty This allows one to temporarily a set variable for a brief duration -- and restore the variable's original value afterward. ---------------------------------------- * CRM-13580: Set translation domain implicitly in extensions http://issues.civicrm.org/jira/browse/CRM-13580 --- CRM/Core/Smarty/plugins/block.crmScope.php | 50 +++++++++++++++++++ .../CRM/Core/Smarty/plugins/CrmScopeTest.php | 39 +++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 CRM/Core/Smarty/plugins/block.crmScope.php create mode 100644 tests/phpunit/CRM/Core/Smarty/plugins/CrmScopeTest.php diff --git a/CRM/Core/Smarty/plugins/block.crmScope.php b/CRM/Core/Smarty/plugins/block.crmScope.php new file mode 100644 index 0000000000..b02efd10fe --- /dev/null +++ b/CRM/Core/Smarty/plugins/block.crmScope.php @@ -0,0 +1,50 @@ +get_template_vars(); + $backupFrame = array(); + foreach ($params as $key => $value) { + $backupFrame[$key] = isset($vars[$key]) ? $vars[$key] : NULL; + } + $backupFrames[] = $backupFrame; + _smarty_block_crmScope_applyFrame($smarty, $params); + } + else { + // close crmScope + _smarty_block_crmScope_applyFrame($smarty, array_pop($backupFrames)); + } + + return $content; +} + +function _smarty_block_crmScope_applyFrame(&$smarty, $frame) { + foreach ($frame as $key => $value) { + $smarty->assign($key, $value); + } +} \ No newline at end of file diff --git a/tests/phpunit/CRM/Core/Smarty/plugins/CrmScopeTest.php b/tests/phpunit/CRM/Core/Smarty/plugins/CrmScopeTest.php new file mode 100644 index 0000000000..56ce3d1165 --- /dev/null +++ b/tests/phpunit/CRM/Core/Smarty/plugins/CrmScopeTest.php @@ -0,0 +1,39 @@ +fetch('string:' . $input); + $this->assertEquals($expected, $actual, "Process input=[$input]"); + } + +} -- 2.25.1