Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "anchored dot notation" #129

Merged
merged 1 commit into from Aug 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Mustache/Context.php
Expand Up @@ -128,7 +128,12 @@ public function findDot($id)
{
$chunks = explode('.', $id);
$first = array_shift($chunks);
$value = $this->findVariableInStack($first, $this->stack);

if ($first === '') {
$value = $this->last();
} else {
$value = $this->findVariableInStack($first, $this->stack);
}

foreach ($chunks as $chunk) {
if ($value === '') {
Expand Down
43 changes: 43 additions & 0 deletions test/Mustache/Test/ContextTest.php
Expand Up @@ -119,6 +119,49 @@ public function testAccessorPriority()
$this->assertEquals('win', $context->find('baz'), 'ArrayAccess stands alone');
$this->assertEquals('win', $context->find('qux'), 'ArrayAccess beats private property');
}

public function testAnchoredDotNotation()
{
$context = new Mustache_Context();

$a = array(
'name' => 'a',
'number' => 1,
);

$b = array(
'number' => 2,
'child' => array(
'name' => 'baby bee',
),
);

$c = array(
'name' => 'cee',
);

$context->push($a);
$this->assertEquals('a', $context->find('name'));
$this->assertEquals('a', $context->findDot('.name'));
$this->assertEquals(1, $context->find('number'));
$this->assertEquals(1, $context->findDot('.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('baby bee', $context->findDot('child.name'));
$this->assertEquals('baby bee', $context->findDot('.child.name'));

$context->push($c);
$this->assertEquals('cee', $context->find('name'));
$this->assertEquals('cee', $context->findDot('.name'));
$this->assertEquals(2, $context->find('number'));
$this->assertEquals('', $context->findDot('.number'));
$this->assertEquals('baby bee', $context->findDot('child.name'));
$this->assertEquals('', $context->findDot('.child.name'));
}
}

class Mustache_Test_TestDummy
Expand Down