ec45c53a5e3c5532c255235567c557f727166698
[civicrm-core.git] / CRM / Core / Smarty / plugins / block.crmScope.php
1 <?php
2
3 /**
4 * Smarty block function to temporarily define variables.
5 *
6 * Example:
7 *
8 * @code
9 * {tsScope x=1}
10 * Expect {$x}==1
11 * {tsScope x=2}
12 * Expect {$x}==2
13 * {/tsScope}
14 * Expect {$x}==1
15 * {/tsScope}
16 * @endcode
17 *
18 * @param array $params must define 'name'
19 * @param string $content Default content
20 * @param object $smarty the Smarty object
21 *
22 * @param $repeat
23 *
24 * @return string
25 */
26 function smarty_block_crmScope($params, $content, &$smarty, &$repeat) {
27 // A list of variables/values to save temporarily
28 static $backupFrames = array();
29
30 if ($repeat) {
31 // open crmScope
32 $vars = $smarty->get_template_vars();
33 $backupFrame = array();
34 foreach ($params as $key => $value) {
35 $backupFrame[$key] = isset($vars[$key]) ? $vars[$key] : NULL;
36 }
37 $backupFrames[] = $backupFrame;
38 _smarty_block_crmScope_applyFrame($smarty, $params);
39 }
40 else {
41 // close crmScope
42 _smarty_block_crmScope_applyFrame($smarty, array_pop($backupFrames));
43 }
44
45 return $content;
46 }
47
48 function _smarty_block_crmScope_applyFrame(&$smarty, $frame) {
49 foreach ($frame as $key => $value) {
50 $smarty->assign($key, $value);
51 }
52 }