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

Implement inverse blocks #29

Merged
merged 4 commits into from
Oct 22, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/main/php/com/handlebarsjs/HandlebarsParser.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
TemplateFormatException,
ParseState
};
use lang\MethodNotImplementedException;
use text\Tokenizer;

/**
Expand Down Expand Up @@ -191,13 +190,17 @@ protected function initialize() {
});

// ^ is either an else by its own, or a negated block
$this->withHandler('^', true, function($tag, $state) {
if ('^' === trim($tag)) {
$this->withHandler('^', true, function($tag, $state, $parse) {
$tag= trim($tag);
if ('^' === $tag) {
$block= cast($state->parents[sizeof($state->parents) - 1], BlockNode::class);
$state->target= $block->inverse();
return;
} else {
$state->parents[]= $state->target;
$block= new InverseOf($this->blocks->newInstance($parse->options(substr($tag, 1)), $state));
$state->target= $block->fn();
$state->parents[]= $block;
}
throw new MethodNotImplementedException('^blocks not yet implemented');
});

// Default
Expand Down
17 changes: 17 additions & 0 deletions src/main/php/com/handlebarsjs/InverseOf.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php namespace com\handlebarsjs;

/** @test com.handlebarsjs.unittest.InverseOfTest */
class InverseOf extends BlockNode {

/** Creates the inverse of a given block */
public function __construct(parent $block) {
parent::__construct(
$block->name,
$block->options,
/* fn: */ $block->inverse,
/* inverse: */ $block->fn,
$block->start,
$block->end
);
}
}
94 changes: 94 additions & 0 deletions src/test/php/com/handlebarsjs/unittest/InverseOfTest.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php namespace com\handlebarsjs\unittest;

use test\{Assert, Test, Values};

class InverseOfTest extends HelperTest {

#[Test, Values([[null, 'Guest'], ['Test', 'User Test']])]
public function inverse_variable($input, $outcome) {
Assert::equals($outcome, $this->evaluate(
'{{#person}}User {{.}}{{/person}}{{^person}}Guest{{/person}}',
['person' => $input]
));
}

#[Test, Values([[null, 'Guest'], ['Test', 'User Test']])]
public function inverse_variable_with_else($input, $outcome) {
Assert::equals($outcome, $this->evaluate(
'{{#person}}User {{.}}{{else}}Guest{{/person}}',
['person' => $input]
));
}

#[Test, Values([[null, 'Guest'], ['Test', 'User Test']])]
public function inverse_variable_with_short_else($input, $outcome) {
Assert::equals($outcome, $this->evaluate(
'{{#person}}User {{.}}{{^}}Guest{{/person}}',
['person' => $input]
));
}

#[Test, Values([[null, 'Guest'], [['name' => 'Test'], 'User Test']])]
public function inverse_section($input, $outcome) {
Assert::equals($outcome, $this->evaluate(
'{{#person}}User {{name}}{{/person}}{{^person}}Guest{{/person}}',
['person' => $input]
));
}

#[Test, Values([[null, 'Guest'], [['name' => 'Test'], 'User Test']])]
public function inverse_section_with_else($input, $outcome) {
Assert::equals($outcome, $this->evaluate(
'{{#person}}User {{name}}{{else}}Guest{{/person}}',
['person' => $input]
));
}

#[Test, Values([[null, 'Guest'], [['name' => 'Test'], 'User Test']])]
public function inverse_section_with_short_else($input, $outcome) {
Assert::equals($outcome, $this->evaluate(
'{{#person}}User {{name}}{{^}}Guest{{/person}}',
['person' => $input]
));
}

#[Test, Values([[[], 'no people'], [['A', 'B'], 'AB']])]
public function inverse_iterator($input, $outcome) {
Assert::equals($outcome, $this->evaluate(
'{{#people}}{{this}}{{/people}}{{^people}}no people{{/people}}',
['people' => $input]
));
}

#[Test, Values([[[], 'no people'], [['A', 'B'], 'AB']])]
public function inverse_iterator_with_else($input, $outcome) {
Assert::equals($outcome, $this->evaluate(
'{{#people}}{{this}}{{else}}no people{{/people}}',
['people' => $input]
));
}

#[Test, Values([[[], 'no people'], [['A', 'B'], 'AB']])]
public function inverse_iterator_with_short_else($input, $outcome) {
Assert::equals($outcome, $this->evaluate(
'{{#people}}{{this}}{{^}}no people{{/people}}',
['people' => $input]
));
}

#[Test, Values([[[], 'no people'], [['A', 'B'], 'some people']])]
public function inverse_if_with_else($input, $outcome) {
Assert::equals($outcome, $this->evaluate(
'{{^if people}}no people{{else}}some people{{/if}}',
['people' => $input]
));
}

#[Test, Values([[[], 'no people'], [['A', 'B'], 'some people']])]
public function inverse_if_with_short_else($input, $outcome) {
Assert::equals($outcome, $this->evaluate(
'{{^if people}}no people{{^}}some people{{/if}}',
['people' => $input]
));
}
}
Loading