segunda-feira, 30 de dezembro de 2019

Microblog application using Quarkus and template (Qute)



Quarkus 1.1 was released and in the announcement surprisingly a new template extension was mentioned: Qute. Qute was made for Quarkus: it works in dev mode and you can compile to native image using Graal. In this post I will share the application I created to test the template engine.


How Qute works with Quarkus?


Qute works like very other Quarkus extension: convention over configuration and simple setup. It has support for custom tags and the template language is very simple.
You can place templates in resource/templates and then inject them in your code. So if you have:

src/main/resources/templates/productTemplate.html

You can inject it using:

@Inject
io.quarkus.qute.Template productTemplate;

Later you can create instances from this template passing variables and return them in JAX-RS methods. Yes, you can use JAX-RS as controller:
 
    @GET
    @Produces(MediaType.TEXT_HTML)
    public TemplateInstance getProductsHtml() {
        return postsTemplate.data("products", products);
    }

And it is not only about HTML, take a look at Qute guide for more information.


The microblog example


We want to create a page where you can make quick posts, for this a single entity Micropost which will be handled by MicropostResource and rendered by postsTemplate.html. 

Micropost is a Panache entity, which makes very easier to access databases;
MicropostResource is a JAX-RS resource and makes use of Qute templates
postsTemplate.html is a simple HTML file that prints a list of posts

Everything is put together by index.html. See the sources below:

Next steps


The application can evolve to add more quarkus extension and learn more (validation, security, more CRUD operations and so on), but for me it was enough to see Qute in action. For it was a great step towards making microfrontends easier to implement using Quarkus.

The code is on my github.





Nenhum comentário:

Postar um comentário