From: Tim Otten Date: Thu, 10 Jul 2014 03:10:35 +0000 (-0700) Subject: crmScope - Move logic to CRM_Core_Smarty X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=17f267d65ecff9abf292a02b095469bbd9ca3c8f;p=civicrm-core.git crmScope - Move logic to CRM_Core_Smarty Currently, you can enter/exit scopes with Smarty tags. This patch allows you to also enter/exit scopes using PHP. --- diff --git a/CRM/Core/Smarty.php b/CRM/Core/Smarty.php index a29d2e231f..9396326f3c 100644 --- a/CRM/Core/Smarty.php +++ b/CRM/Core/Smarty.php @@ -73,6 +73,11 @@ class CRM_Core_Smarty extends Smarty { */ static private $_singleton = NULL; + /** + * @var array (string $name => mixed $value) a list of variables ot save temporarily + */ + private $backupFrames = array(); + /** * class constructor * @@ -245,5 +250,56 @@ class CRM_Core_Smarty extends Smarty { } } + + /** + * Temporarily assign a list of variables. + * + * @code + * $smarty->pushScope(array( + * 'first_name' => 'Alice', + * 'last_name' => 'roberts', + * )); + * $html = $smarty->fetch('view-contact.tpl'); + * $smarty->popScope(); + * @endcode + * + * @param array $vars (string $name => mixed $value) + * @return CRM_Core_Smarty + * @see popScope + */ + public function pushScope($vars) { + $oldVars = $this->get_template_vars(); + $backupFrame = array(); + foreach ($vars as $key => $value) { + $backupFrame[$key] = isset($oldVars[$key]) ? $oldVars[$key] : NULL; + } + $this->backupFrames[] = $backupFrame; + + $this->assignAll($vars); + + return $this; + } + + /** + * Remove any values that were previously pushed. + * + * @return CRM_Core_Smarty + * @see pushScope + */ + public function popScope() { + $this->assignAll(array_pop($this->backupFrames)); + return $this; + } + + /** + * @param array $vars (string $name => mixed $value) + * @return CRM_Core_Smarty + */ + public function assignAll($vars) { + foreach ($vars as $key => $value) { + $this->assign($key, $value); + } + return $this; + } } diff --git a/CRM/Core/Smarty/plugins/block.crmScope.php b/CRM/Core/Smarty/plugins/block.crmScope.php index b37050b326..4dc15b8e9c 100644 --- a/CRM/Core/Smarty/plugins/block.crmScope.php +++ b/CRM/Core/Smarty/plugins/block.crmScope.php @@ -24,33 +24,16 @@ * @return string */ function smarty_block_crmScope($params, $content, &$smarty, &$repeat) { - // A list of variables/values to save temporarily - static $backupFrames = array(); + /** @var CRM_Core_Smarty $smarty */ if ($repeat) { // open crmScope - $vars = $smarty->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); + $smarty->pushScope($params); } else { // close crmScope - _smarty_block_crmScope_applyFrame($smarty, array_pop($backupFrames)); + $smarty->popScope(); } return $content; } - -/** - * @param $smarty - * @param $frame - */ -function _smarty_block_crmScope_applyFrame(&$smarty, $frame) { - foreach ($frame as $key => $value) { - $smarty->assign($key, $value); - } -}