Skip to content

Commit

Permalink
Merge pull request #15472 from mkouba/qute-letsection-docs
Browse files Browse the repository at this point in the history
Qute - clarify that literals can be used in Let/Set section
  • Loading branch information
mkouba authored Mar 8, 2021
2 parents dbf5d5d + 4a0dec8 commit 4a92ef5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
9 changes: 6 additions & 3 deletions docs/src/main/asciidoc/qute-reference.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ NOTE: The current context can be accessed via the implicit binding `this`.

|Elvis Operator
|Outputs the default value if the previous part cannot be resolved or resolves to `null`.
|`{person.name ?: 'John'}`, `{person.name or 'John'}`
|`{person.name ?: 'John'}`, `{person.name or 'John'}`, `{person.name.or('John')}`

|orEmpty
|Outputs an empty list if the previous part cannot be resolved or resolves to `null`.
Expand All @@ -370,7 +370,7 @@ NOTE: The current context can be accessed via the implicit binding `this`.

TIP: The condition in a ternary operator evaluates to `true` if the value is not considered `falsy` as described in the <<if_section>>.

NOTE: In fact, the operators are implemented as "virtual methods" that consume one parameter and can be used with infix notation, i.e. `{person.name or 'John'}` is translated to `{person.name.or('John')}`.
NOTE: In fact, the operators are implemented as "virtual methods" that consume one parameter and can be used with infix notation. For example `{person.name or 'John'}` is translated to `{person.name.or('John')}` and `{item.isActive ? item.name : 'Inactive item'}` is translated to `{item.isActive.ifTruthy(item.name).or('Inactive item')}`

==== Arrays

Expand Down Expand Up @@ -780,10 +780,13 @@ This section might also come in handy when we'd like to avoid multiple expensive
This section allows you to define named local variables:
[source,html]
----
{#let myParent=order.item.parent}
{#let myParent=order.item.parent isActive=false age=10} <1>
<h1>{myParent.name}</h1>
Is active: {isActive}
Age: {age}
{/let}
----
<1> The local variable is initialized with an expression that can also represent a <<literals,literal>>.

The section tag is also registered under the `set` alias:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1100,7 +1100,7 @@ TemplateVariantsBuildItem collectTemplateVariants(List<TemplatePathBuildItem> te
@BuildStep
void excludeTypeChecks(QuteConfig config, BuildProducer<TypeCheckExcludeBuildItem> excludes) {
// Exclude all checks that involve built-in value resolvers
List<String> skipOperators = Arrays.asList("?:", "or", ":", "?", "&&", "||");
List<String> skipOperators = Arrays.asList("?:", "or", ":", "?", "ifTruthy", "&&", "||");
excludes.produce(new TypeCheckExcludeBuildItem(new Predicate<TypeCheck>() {
@Override
public boolean test(TypeCheck check) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public CompletionStage<Object> resolve(EvalContext context) {
}

/**
* Returns the default value if the base object is null or {@link Result#NOT_FOUND}.
* Returns the default value if the base object is null or {@link Result#NOT_FOUND} and the base object otherwise.
*
* {@code foo.or(bar)}, {@code foo or true}, {@code name ?: 'elvis'}
*/
Expand Down Expand Up @@ -133,6 +133,8 @@ public CompletionStage<Object> resolve(EvalContext context) {
}

/**
* Returns {@link Result#NOT_FOUND} if the base object is falsy and the base object otherwise.
* <p>
* Can be used together with {@link #orResolver()} to form a ternary operator.
*
* {@code person.isElvis ? 'elvis' : notElvis}
Expand All @@ -141,8 +143,16 @@ public static ValueResolver trueResolver() {
return new ValueResolver() {

public boolean appliesTo(EvalContext context) {
return context.getParams().size() == 1
&& ("?".equals(context.getName()));
if (context.getParams().size() != 1) {
return false;
}
switch (context.getName()) {
case "?":
case "ifTruthy":
return true;
default:
return false;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,15 @@ public void testLet() {
engine.parse("{foo}:{baz} - {#let foo=true bar='mix'}{foo}:{bar}:{baz}{/}").data("baz", "what?!").render());
}

@Test
public void testLiterals() {
Engine engine = Engine.builder().addDefaults().addValueResolver(new ReflectionValueResolver()).build();
assertEquals("1::4::Andy::false",
engine.parse(
"{#let foo=1 bar='qute' baz=name.or('Andy') alpha=name.ifTruthy('true').or('false')}"
+ "{#for i in foo}{count}{/for}::{bar.length}::{baz}::{alpha}"
+ "{/let}")
.render());
}

}

0 comments on commit 4a92ef5

Please sign in to comment.