*/
static private $_singleton = NULL;
+ /**
+ * @var array (string $name => mixed $value) a list of variables ot save temporarily
+ */
+ private $backupFrames = array();
+
/**
* class constructor
*
}
}
+
+ /**
+ * 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;
+ }
}
* @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);
- }
-}