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

Qute - register param expressions for includes and user-defined tags #18930

Merged
merged 1 commit into from
Jul 23, 2021
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
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());
}

}