Skip to content

Commit

Permalink
Qute: execute tag templates in isolation by default
Browse files Browse the repository at this point in the history
  • Loading branch information
mkouba committed Aug 28, 2023
1 parent 3dede5c commit 647b5ca
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 18 deletions.
8 changes: 4 additions & 4 deletions docs/src/main/asciidoc/qute-reference.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1143,10 +1143,10 @@ Then, we can call the tag like this:
<1> `item` is resolved to an iteration element and can be referenced using the `it` key in the tag template.
<2> Tag content injected using the `nested-content` key in the tag template.

By default, the tag template can reference data from the parent context.
For example, the tag above could use the following expression `{items.size}`.
However, sometimes it might be useful to disable this behavior and execute the tag as an _isolated_ template, i.e. without access to the context of the template that calls the tag.
In this case, just add `_isolated` or `_isolated=true` argument to the call site, e.g. `{#itemDetail item showImage=true _isolated /}`.
By default, a tag template cannot reference the data from the parent context.
Qute executes the tag as an _isolated_ template, i.e. without access to the context of the template that calls the tag.
However, sometimes it might be useful to change the default behavior and disable the isolation.
In this case, just add `_isolated=false` to the call site, for example `{#itemDetail item showImage=true _isolated=false /}`.

User tags can also make use of the template inheritance in the same way as regular `{#include}` sections do.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,9 @@ protected String getTemplateId(SectionInitContext context) {

@Override
protected IncludeSectionHelper newHelper(Supplier<Template> template, Map<String, Expression> params,
Map<String, SectionBlock> extendingBlocks, boolean isolated, SectionInitContext context) {
return new IncludeSectionHelper(template, extendingBlocks, params, isolated);
Map<String, SectionBlock> extendingBlocks, boolean isolatedValue, SectionInitContext context) {
return new IncludeSectionHelper(template, extendingBlocks, params, isolatedValue ? true
: Boolean.parseBoolean(context.getParameterOrDefault(ISOLATED, ISOLATED_DEFAULT_VALUE)));
}

}
Expand All @@ -151,17 +152,21 @@ public boolean treatUnknownSectionsAsBlocks() {
return true;
}

String isolatedDefaultValue() {
return ISOLATED_DEFAULT_VALUE;
}

void addDefaultParams(ParametersInfo.Builder builder) {
builder
.addParameter(Parameter.builder(ISOLATED).defaultValue(ISOLATED_DEFAULT_VALUE).optional()
.addParameter(Parameter.builder(ISOLATED).defaultValue(isolatedDefaultValue()).optional()
.valuePredicate(new Predicate<String>() {

@Override
public boolean test(String v) {
return ISOLATED.equals(v);
}
}).build())
.addParameter(Parameter.builder(IGNORE_FRAGMENTS).defaultValue(ISOLATED_DEFAULT_VALUE).optional()
.addParameter(Parameter.builder(IGNORE_FRAGMENTS).defaultValue("false").optional()
.valuePredicate(new Predicate<String>() {

@Override
Expand Down Expand Up @@ -292,9 +297,7 @@ public Template get() {
return template;
}
};

return newHelper(template, params, extendingBlocks, isolatedValue ? true
: Boolean.parseBoolean(context.getParameterOrDefault(ISOLATED, ISOLATED_DEFAULT_VALUE)), context);
return newHelper(template, params, extendingBlocks, isolatedValue, context);
}

protected abstract String getTemplateId(SectionInitContext context);
Expand Down Expand Up @@ -342,7 +345,7 @@ protected boolean isSinglePart(String value) {
protected abstract boolean ignoreParameterInit(String key, String value);

protected abstract T newHelper(Supplier<Template> template, Map<String, Expression> params,
Map<String, SectionBlock> extendingBlocks, boolean isolated, SectionInitContext context);
Map<String, SectionBlock> extendingBlocks, boolean isolatedValue, SectionInitContext context);
}

enum Code implements ErrorCode {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ private boolean isNestedContent(Expression expr) {
public static class Factory extends IncludeSectionHelper.AbstractIncludeFactory<UserTagSectionHelper> {

private static final String IT = "it";
// Unlike regular includes user tags are isolated by default
private static final String ISOLATED_DEFAULT_VALUE = "true";

private final String name;
private final String templateId;
Expand All @@ -61,6 +63,11 @@ public List<String> getDefaultAliases() {
return ImmutableList.of(name);
}

@Override
String isolatedDefaultValue() {
return ISOLATED_DEFAULT_VALUE;
}

@Override
public ParametersInfo getParameters() {
ParametersInfo.Builder builder = ParametersInfo.builder().addParameter(Parameter.builder(IT).defaultValue(IT));
Expand All @@ -85,9 +92,12 @@ protected String getTemplateId(SectionInitContext context) {

@Override
protected UserTagSectionHelper newHelper(Supplier<Template> template, Map<String, Expression> params,
Map<String, SectionBlock> extendingBlocks, boolean isolated, SectionInitContext context) {
Map<String, SectionBlock> extendingBlocks, boolean isolatedValue, SectionInitContext context) {
boolean isNestedContentNeeded = !context.getBlock(SectionHelperFactory.MAIN_BLOCK_NAME).isEmpty();
return new UserTagSectionHelper(template, extendingBlocks, params, isolated, isNestedContentNeeded);
return new UserTagSectionHelper(template, extendingBlocks, params,
isolatedValue ? true
: Boolean.parseBoolean(context.getParameterOrDefault(ISOLATED, ISOLATED_DEFAULT_VALUE)),
isNestedContentNeeded);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,17 @@ public void testOptionalEndTag() {
engine.parse("{#include super}{#let foo=1} {foo}").render());
}

@Test
public void testIsolation() {
Engine engine = Engine.builder()
.addDefaults()
.strictRendering(false)
.build();

Template foo = engine.parse("{name}");
engine.putTemplate("foo", foo);
assertEquals("Dorka", engine.parse("{#include foo /}").data("name", "Dorka").render());
assertEquals("NOT_FOUND", engine.parse("{#include foo _isolated /}").data("name", "Dorka").render());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public void testUserTag() {
assertEquals("nope",
engine.parse("{#each this}{#myTag showImage=false /}{/each}").render(Collections.singletonMap("order", order)));
assertEquals("Herbert",
engine.parse("{#each this}{#myTag it showImage=true /}{/each}").render(Collections.singletonList(order)));
engine.parse("{#each this}{#myTag it showImage=true _isolated=false /}{/each}")
.render(Collections.singletonList(order)));
}

@Test
Expand All @@ -45,15 +46,16 @@ public void testUserTagWithNestedContent() {
Map<String, Object> order = new HashMap<>();
order.put("name", "Herbert");
assertEquals("<b>Herbert</b>",
engine.parse("{#myTag showImage=true}{order.name}{/}").render(Collections.singletonMap("order", order)));
engine.parse("{#myTag showImage=true _isolated=false}{order.name}{/}")
.render(Collections.singletonMap("order", order)));
assertEquals("nope", engine.parse("{#myTag}{order.name}{/}").render(Collections.singletonMap("order", order)));
assertEquals("nope",
engine.parse("{#myTag showImage=false}{order.name}{/}").render(Collections.singletonMap("order", order)));
assertEquals("nope",
engine.parse("{#each this}{#myTag showImage=false}{it.name}{/}{/each}")
.render(Collections.singletonMap("order", order)));
assertEquals("<b>Herbert</b>",
engine.parse("{#each this}{#myTag showImage=true}{it.name}{/}{/each}")
engine.parse("{#each this}{#myTag showImage=true _isolated=false}{it.name}{/}{/each}")
.render(Collections.singletonList(order)));
}

Expand Down Expand Up @@ -105,7 +107,7 @@ public void testEval() {
engine.putTemplate("my-tag-id", tag);

assertEquals("10 kg",
engine.parse("{#itemDetail itemId=1 myNestedContent=\"{item.quantity} {item.unit}\" /}")
engine.parse("{#itemDetail itemId=1 myNestedContent=\"{item.quantity} {item.unit}\" _isolated=false /}")
.data("items", Map.of(1, Map.of("quantity", 10, "unit", "kg"))).render());
}

Expand Down Expand Up @@ -140,4 +142,18 @@ public void testInsertSections() {
assertEquals("Baz!", engine.parse("{#myTag2}Baz!{/myTag2}").render());
}

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

Template tag = engine.parse("{name}");
engine.putTemplate("my-tag-id", tag);
assertEquals("NOT_FOUND", engine.parse("{#myTag /}").data("name", "Dorka").render());
assertEquals("Dorka", engine.parse("{#myTag _isolated=false /}").data("name", "Dorka").render());
}

}

0 comments on commit 647b5ca

Please sign in to comment.