Skip to content
Snippets Groups Projects
Commit 9915ae36 authored by totten's avatar totten
Browse files

CRM-13580 - crmScope - Add support for "dynamic variable scopes" in Smarty

This allows one to temporarily a set variable for a brief duration -- and
restore the variable's original value afterward.

----------------------------------------
* CRM-13580: Set translation domain implicitly in extensions
  http://issues.civicrm.org/jira/browse/CRM-13580
parent 479e0bc3
No related branches found
No related tags found
No related merge requests found
<?php
/**
* Smarty block function to temporarily define variables.
*
* Example:
*
* @code
* {tsScope x=1}
* Expect {$x}==1
* {tsScope x=2}
* Expect {$x}==2
* {/tsScope}
* Expect {$x}==1
* {/tsScope}
* @endcode
*
* @param array $params must define 'name'
* @param string $content Default content
* @param object $smarty the Smarty object
*
* @return string
*/
function smarty_block_crmScope($params, $content, &$smarty, &$repeat) {
// A list of variables/values to save temporarily
static $backupFrames = array();
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);
}
else {
// close crmScope
_smarty_block_crmScope_applyFrame($smarty, array_pop($backupFrames));
}
return $content;
}
function _smarty_block_crmScope_applyFrame(&$smarty, $frame) {
foreach ($frame as $key => $value) {
$smarty->assign($key, $value);
}
}
\ No newline at end of file
<?php
require_once 'CiviTest/CiviUnitTestCase.php';
class CRM_Core_Smarty_plugins_CrmScopeTest extends CiviUnitTestCase {
function setUp() {
parent::setUp();
require_once 'CRM/Core/Smarty.php';
// Templates should normally be file names, but for unit-testing it's handy to use "string:" notation
require_once 'CRM/Core/Smarty/resources/String.php';
civicrm_smarty_register_string_resource();
}
function scopeCases() {
$cases = array();
$cases[] = array('', '{crmScope}{/crmScope}');
$cases[] = array('', '{crmScope x=1}{/crmScope}');
$cases[] = array('x=', 'x={$x}');
$cases[] = array('x=1', '{crmScope x=1}x={$x}{/crmScope}');
$cases[] = array('x=1', '{$x}{crmScope x=1}x={$x}{/crmScope}{$x}');
$cases[] = array('x=1 x=2 x=1', '{crmScope x=1}x={$x} {crmScope x=2}x={$x}{/crmScope} x={$x}{/crmScope}');
$cases[] = array('x=1 x=2 x=3 x=2 x=1', '{crmScope x=1}x={$x} {crmScope x=2}x={$x} {crmScope x=3}x={$x}{/crmScope} x={$x}{/crmScope} x={$x}{/crmScope}');
$cases[] = array('x=1,y=9', '{crmScope x=1 y=9}x={$x},y={$y}{/crmScope}');
$cases[] = array('x=1,y=9 x=1,y=8 x=1,y=9', '{crmScope x=1 y=9}x={$x},y={$y} {crmScope y=8}x={$x},y={$y}{/crmScope} x={$x},y={$y}{/crmScope}');
$cases[] = array('x=', 'x={$x}');
return $cases;
}
/**
* @dataProvider scopeCases
*/
function testBlank($expected, $input) {
$smarty = CRM_Core_Smarty::singleton();
$actual = $smarty->fetch('string:' . $input);
$this->assertEquals($expected, $actual, "Process input=[$input]");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment