Put anchored dots behind {{%ANCHORED-DOT}} pragma.

This commit is contained in:
Justin Hileman
2015-08-15 12:18:04 -07:00
parent 664895f4b9
commit 1b1757987a
4 changed files with 70 additions and 13 deletions
+6
View File
@@ -631,6 +631,12 @@ class Mustache_Compiler
return 'last';
}
if (isset($this->pragmas[Mustache_Engine::PRAGMA_ANCHORED_DOT]) && $this->pragmas[Mustache_Engine::PRAGMA_ANCHORED_DOT]) {
if (substr($id, 0, 1) === '.') {
return 'findAnchoredDot';
}
}
if (strpos($id, '.') === false) {
return 'find';
}
+35 -4
View File
@@ -128,13 +128,44 @@ class Mustache_Context
{
$chunks = explode('.', $id);
$first = array_shift($chunks);
if ($first === '') {
$value = $this->last();
} else {
$value = $this->findVariableInStack($first, $this->stack);
foreach ($chunks as $chunk) {
if ($value === '') {
return $value;
}
$value = $this->findVariableInStack($chunk, array($value));
}
return $value;
}
/**
* Find an 'anchored dot notation' variable in the Context stack.
*
* This is the same as findDot(), except it looks in the top of the context
* stack for the first value, rather than searching the whole context stack
* and starting from there.
*
* @see Mustache_Context::findDot
*
* @throws Mustache_Exception_InvalidArgumentException if given an invalid anchored dot $id.
*
* @param string $id Dotted variable selector
*
* @return mixed Variable value, or '' if not found
*/
public function findAnchoredDot($id)
{
$chunks = explode('.', $id);
$first = array_shift($chunks);
if ($first !== '') {
throw new Mustache_Exception_InvalidArgumentException(sprintf('Unexpected id for findAnchoredDot: %s', $id));
}
$value = $this->last();
foreach ($chunks as $chunk) {
if ($value === '') {
return $value;
+2
View File
@@ -28,11 +28,13 @@ class Mustache_Engine
const PRAGMA_FILTERS = 'FILTERS';
const PRAGMA_BLOCKS = 'BLOCKS';
const PRAGMA_ANCHORED_DOT = 'ANCHORED-DOT';
// Known pragmas
private static $knownPragmas = array(
self::PRAGMA_FILTERS => true,
self::PRAGMA_BLOCKS => true,
self::PRAGMA_ANCHORED_DOT => true,
);
// Template cache
+23 -5
View File
@@ -142,25 +142,43 @@ class Mustache_Test_ContextTest extends PHPUnit_Framework_TestCase
$context->push($a);
$this->assertEquals('a', $context->find('name'));
$this->assertEquals('a', $context->findDot('.name'));
$this->assertEquals('', $context->findDot('.name'));
$this->assertEquals('a', $context->findAnchoredDot('.name'));
$this->assertEquals(1, $context->find('number'));
$this->assertEquals(1, $context->findDot('.number'));
$this->assertEquals('', $context->findDot('.number'));
$this->assertEquals(1, $context->findAnchoredDot('.number'));
$context->push($b);
$this->assertEquals('a', $context->find('name'));
$this->assertEquals(2, $context->find('number'));
$this->assertEquals('', $context->findDot('.name'));
$this->assertEquals(2, $context->findDot('.number'));
$this->assertEquals('', $context->findDot('.number'));
$this->assertEquals('', $context->findAnchoredDot('.name'));
$this->assertEquals(2, $context->findAnchoredDot('.number'));
$this->assertEquals('baby bee', $context->findDot('child.name'));
$this->assertEquals('baby bee', $context->findDot('.child.name'));
$this->assertEquals('', $context->findDot('.child.name'));
$this->assertEquals('baby bee', $context->findAnchoredDot('.child.name'));
$context->push($c);
$this->assertEquals('cee', $context->find('name'));
$this->assertEquals('cee', $context->findDot('.name'));
$this->assertEquals('', $context->findDot('.name'));
$this->assertEquals('cee', $context->findAnchoredDot('.name'));
$this->assertEquals(2, $context->find('number'));
$this->assertEquals('', $context->findDot('.number'));
$this->assertEquals('', $context->findAnchoredDot('.number'));
$this->assertEquals('baby bee', $context->findDot('child.name'));
$this->assertEquals('', $context->findDot('.child.name'));
$this->assertEquals('', $context->findAnchoredDot('.child.name'));
}
/**
* @expectedException Mustache_Exception_InvalidArgumentException
*/
public function testAnchoredDotNotationThrowsExceptions()
{
$context = new Mustache_Context();
$context->push(array('a' => 1));
$context->findAnchoredDot('a');
}
}