Commit | Line | Data |
---|---|---|
9915ae36 TO |
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 | * | |
77b97be7 EM |
18 | * @param array $params must define 'name' |
19 | * @param string $content Default content | |
20 | * @param object $smarty the Smarty object | |
21 | * | |
22 | * @param $repeat | |
9915ae36 TO |
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 | ||
a0ee3941 EM |
48 | /** |
49 | * @param $smarty | |
50 | * @param $frame | |
51 | */ | |
9915ae36 TO |
52 | function _smarty_block_crmScope_applyFrame(&$smarty, $frame) { |
53 | foreach ($frame as $key => $value) { | |
54 | $smarty->assign($key, $value); | |
55 | } | |
77b97be7 | 56 | } |