X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FCore%2FSmarty.php;h=7c941d8cba681478b38db85af7cbf4150a5c57e9;hb=2aa397bc9ba4608d4146278d55dedd4080c53aec;hp=0da5594db86f1089de2dc7934c8430719417036b;hpb=d5f6077993b1df66a934933af0bc9327cff13e78;p=civicrm-core.git diff --git a/CRM/Core/Smarty.php b/CRM/Core/Smarty.php index 0da5594db8..7c941d8cba 100644 --- a/CRM/Core/Smarty.php +++ b/CRM/Core/Smarty.php @@ -1,7 +1,7 @@ mixed $value) a list of variables ot save temporarily + */ + private $backupFrames = array(); + + /** + * Class constructor * * @return CRM_Core_Smarty - * @access private */ private function __construct() { parent::__construct(); } - private function initialize( ) { + private function initialize() { $config = CRM_Core_Config::singleton(); if (isset($config->customTemplateDir) && $config->customTemplateDir) { @@ -172,7 +176,7 @@ class CRM_Core_Smarty extends Smarty { * Method providing static instance of SmartTemplate, as * in Singleton pattern. */ - static function &singleton() { + public static function &singleton() { if (!isset(self::$_singleton)) { self::$_singleton = new CRM_Core_Smarty( ); self::$_singleton->initialize( ); @@ -183,16 +187,16 @@ class CRM_Core_Smarty extends Smarty { } /** - * executes & returns or displays the template results + * Executes & returns or displays the template results * * @param string $resource_name * @param string $cache_id * @param string $compile_id - * @param boolean $display + * @param bool $display * * @return bool|mixed|string */ - function fetch($resource_name, $cache_id = NULL, $compile_id = NULL, $display = FALSE) { + public function fetch($resource_name, $cache_id = NULL, $compile_id = NULL, $display = FALSE) { if (preg_match( '/^(\s+)?string:/', $resource_name)) { $old_security = $this->security; $this->security = TRUE; @@ -204,7 +208,33 @@ class CRM_Core_Smarty extends Smarty { return $output; } - function appendValue($name, $value) { + /** + * Fetch a template (while using certain variables) + * + * @param string $resource_name + * @param array $vars + * (string $name => mixed $value) variables to export to Smarty. + * @throws Exception + * @return bool|mixed|string + */ + public function fetchWith($resource_name, $vars) { + $this->pushScope($vars); + try { + $result = $this->fetch($resource_name); + } catch (Exception $e) { + // simulate try { ... } finally { ... } + $this->popScope(); + throw $e; + } + $this->popScope(); + return $result; + } + + /** + * @param string $name + * @param $value + */ + public function appendValue($name, $value) { $currentValue = $this->get_template_vars($name); if (!$currentValue) { $this->assign($name, $value); @@ -216,7 +246,7 @@ class CRM_Core_Smarty extends Smarty { } } - function clearTemplateVars() { + public function clearTemplateVars() { foreach (array_keys($this->_tpl_vars) as $key) { if ($key == 'config' || $key == 'session') { continue; @@ -225,12 +255,15 @@ class CRM_Core_Smarty extends Smarty { } } - static function registerStringResource() { + public static function registerStringResource() { require_once 'CRM/Core/Smarty/resources/String.php'; civicrm_smarty_register_string_resource(); } - function addTemplateDir($path) { + /** + * @param $path + */ + public function addTemplateDir($path) { if ( is_array( $this->template_dir ) ) { array_unshift( $this->template_dir, $path ); } else { @@ -238,5 +271,57 @@ 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; + } +}