Skip to content

Commit

Permalink
Qute - register param expressions for includes and user-defined tags
Browse files Browse the repository at this point in the history
- only registered expression can be validated at build time
  • Loading branch information
mkouba committed Jul 22, 2021
1 parent d7f7573 commit 867ccf8
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ public boolean treatUnknownSectionsAsBlocks() {
return true;
}

@Override
public Scope initializeBlock(Scope outerScope, BlockInfo block) {
if (block.getLabel().equals(MAIN_BLOCK_NAME)) {
for (Entry<String, String> entry : block.getParameters().entrySet()) {
if (!entry.getKey().equals(TEMPLATE)) {
block.addExpression(entry.getKey(), entry.getValue());
}
}
return outerScope;
} else {
return outerScope;
}
}

@Override
public IncludeSectionHelper initialize(SectionInitContext context) {

Expand All @@ -97,7 +111,7 @@ public IncludeSectionHelper initialize(SectionInitContext context) {
params = new HashMap<>();
for (Entry<String, String> entry : context.getParameters().entrySet()) {
if (!entry.getKey().equals(TEMPLATE)) {
params.put(entry.getKey(), context.parseValue(entry.getValue()));
params.put(entry.getKey(), context.getExpression(entry.getKey()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,29 @@ public ParametersInfo getParameters() {
return ParametersInfo.builder().addParameter(new Parameter(IT, IT, false)).build();
}

@Override
public Scope initializeBlock(Scope outerScope, BlockInfo block) {
if (block.getLabel().equals(MAIN_BLOCK_NAME)) {
for (Entry<String, String> entry : block.getParameters().entrySet()) {
if (entry.getKey().equals(IT) && entry.getValue().equals(IT)) {
continue;
}
block.addExpression(entry.getKey(), entry.getValue());
}
return outerScope;
} else {
return outerScope;
}
}

@Override
public UserTagSectionHelper initialize(SectionInitContext context) {
Map<String, Expression> params = new HashMap<>();
for (Entry<String, String> entry : context.getParameters().entrySet()) {
if (entry.getKey().equals(IT) && entry.getValue().equals(IT)) {
continue;
}
params.put(entry.getKey(), context.parseValue(entry.getValue()));
params.put(entry.getKey(), context.getExpression(entry.getKey()));
}
boolean isEmpty = context.getBlocks().size() == 1 && context.getBlocks().get(0).isEmpty();
final Engine engine = context.getEngine();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ public void testEmptyInclude() {
public void testInsertParam() {
Engine engine = Engine.builder().addDefaults().build();
engine.putTemplate("super", engine.parse("{#insert header}default header{/insert} and {#insert footer}{that}{/}"));
assertEquals("1 and 1", engine.parse("{#include 'super' that=foo}{#header}{that}{/}{/}").data("foo", 1).render());
Template foo = engine.parse("{#include 'super' that=foo}{#header}{that}{/}{/}");
// foo, that
assertEquals(2, foo.getExpressions().size());
assertEquals("1 and 1", foo.data("foo", 1).render());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public void testUserTag() {
.build();

Template tag = engine.parse("{#if showImage.or(false)}{it.name}{#else}nope{/if}");
// showImage.or(false), it.name
assertEquals(2, tag.getExpressions().size());
engine.putTemplate("my-tag-id", tag);

Map<String, Object> order = new HashMap<>();
Expand Down Expand Up @@ -72,7 +74,24 @@ public void testUserTagWithRichNestedContent() {
assertEquals("<b><i>Herbert</i></b>",
engine.parse("{#myTag showImage=true}{#myTag2 showImage2=true}{order.name}{/myTag2}{/myTag}")
.render(Collections.singletonMap("order", order)));
}

@Test
public void testUserTagLoopParam() {
Engine engine = Engine.builder().addDefaults().addValueResolver(new ReflectionValueResolver())
.addSectionHelper(new UserTagSectionHelper.Factory("myTag", "my-tag-id"))
.build();

Template tag = engine.parse("{it} {surname}");
engine.putTemplate("my-tag-id", tag);

assertEquals("KOUBA kouba",
engine.parse("{#each surnames}{#myTag it.toUpperCase surname=it.toLowerCase /}{/each}")
.data("surnames", Collections.singleton("Kouba")).render());
assertEquals("KOUBA kouba",
engine.parse(
"{#for surname in surnames}{#each surnames}{#myTag it.toUpperCase surname=surname.toLowerCase /}{/each}{/for}")
.data("surnames", Collections.singleton("Kouba")).render());
}

}

0 comments on commit 867ccf8

Please sign in to comment.