Skip to content

Commit

Permalink
Introduce TemplateInstance.setVariant
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Apr 15, 2024
1 parent 9f66268 commit f0c5435
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
6 changes: 4 additions & 2 deletions docs/src/main/asciidoc/qute-reference.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1659,7 +1659,7 @@ public class CustomLocator implements TemplateLocator {
=== Template Variants

Sometimes it's useful to render a specific variant of the template based on the content negotiation.
This can be done by setting a special attribute via `TemplateInstance.setAttribute()`:
This can be done by setting a special attribute via `TemplateInstance.setVariant()`:

[source,java]
----
Expand All @@ -1672,7 +1672,9 @@ class MyService {
ItemManager manager;
String renderItems() {
return items.data("items",manager.findItems()).setAttribute(TemplateInstance.SELECTED_VARIANT, new Variant(Locale.getDefault(),"text/html","UTF-8")).render();
return items.data("items", manager.findItems())
.setVariant(new Variant(Locale.getDefault(), "text/html", "UTF-8"))
.render();
}
}
----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,16 @@ default TemplateInstance setLocale(Locale locale) {
return setAttribute(LOCALE, locale);
}

/**
* Sets the variant attribute that can be used to select a specific variant of the template.
*
* @param variant the variant
* @return self
*/
default TemplateInstance setVariant(Variant variant) {
return setAttribute(SELECTED_VARIANT, variant);
}

/**
* This component can be used to initialize a template instance, i.e. the data and attributes.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.List;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicBoolean;

Expand Down Expand Up @@ -78,4 +77,21 @@ public void testLocale() throws Exception {
Template hello = engine.parse("Hello {locale}!");
assertEquals("Hello fr!", hello.instance().setLocale(Locale.FRENCH).render());
}

@Test
public void testVariant() {
Engine engine = Engine.builder().addDefaults()
.addValueResolver(ValueResolver.builder()
.applyToName("variant")
.resolveSync(ctx -> ctx.getAttribute(TemplateInstance.SELECTED_VARIANT))
.build())
.addValueResolver(ValueResolver.builder()
.appliesTo(ctx -> ctx.getBase() instanceof Variant && ctx.getName().equals("contentType"))
.resolveSync(ctx -> ((Variant) ctx.getBase()).getContentType())
.build())
.build();
Template hello = engine.parse("Hello {variant.contentType}!");
String render = hello.instance().setVariant(Variant.forContentType(Variant.TEXT_HTML)).render();
assertEquals("Hello text/html!", render);
}
}

0 comments on commit f0c5435

Please sign in to comment.