Merge pull request #2845 from elcapo/activity-contact-api
[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 * @return string
23 */
24 function smarty_block_crmScope($params, $content, &$smarty, &$repeat) {
25 // A list of variables/values to save temporarily
26 static $backupFrames = array();
27
28 if ($repeat) {
29 // open crmScope
30 $vars = $smarty->get_template_vars();
31 $backupFrame = array();
32 foreach ($params as $key => $value) {
33 $backupFrame[$key] = isset($vars[$key]) ? $vars[$key] : NULL;
34 }
35 $backupFrames[] = $backupFrame;
36 _smarty_block_crmScope_applyFrame($smarty, $params);
37 }
38 else {
39 // close crmScope
40 _smarty_block_crmScope_applyFrame($smarty, array_pop($backupFrames));
41 }
42
43 return $content;
44 }
45
46 function _smarty_block_crmScope_applyFrame(&$smarty, $frame) {
47 foreach ($frame as $key => $value) {
48 $smarty->assign($key, $value);
49 }
50 }