How to inject a context-aware common state into Quarkus Qute template?

2 min read 03-09-2024
How to inject a context-aware common state into Quarkus Qute template?


Injecting Context-Aware Common State into Quarkus Qute Templates

Quarkus provides a powerful framework for building modern, reactive applications. One of its key features is the integration with Qute, a fast and efficient templating engine. But how do you inject dynamic data, like the current user, into your Qute templates without cluttering your routes with unnecessary injections? This article will explore the best practices for injecting context-aware common state into your Quarkus Qute templates.

Understanding the Challenge

As highlighted in the original Stack Overflow question [1], we often need to access common data like the current user within our Qute templates. However, the GlobalVariables feature offered by Qute is static, meaning it's not context-aware. This presents a challenge: how do we dynamically inject data that changes depending on the user or the request?

The Solution: Using a CDI Bean

The answer lies in utilizing the power of CDI (Contexts and Dependency Injection) in Quarkus. CDI allows us to define beans that can be injected into various parts of our application, including our Qute templates.

1. Creating a Context-Aware Bean:

First, we need to create a CDI bean that holds the common data we want to access within our templates. This bean will typically be responsible for managing the user information or any other context-specific data.

@ApplicationScoped
public class UserContext {

    @Inject
    SecurityIdentity securityIdentity; 

    public User getCurrentUser() {
        return securityIdentity.getPrincipal();
    }
}

This UserContext bean, annotated with @ApplicationScoped, ensures it's available throughout the application's lifecycle. It uses @Inject to inject the SecurityIdentity, which provides access to the currently authenticated user.

2. Injecting the Bean into Templates:

Now, within our Qute templates, we can inject this UserContext bean directly. This allows us to access the user's data dynamically within our template logic.

<p>Welcome, ${userContext.currentUser.name}!</p>

Here, userContext is the variable name for the injected UserContext bean, and currentUser is the method we defined in our bean to retrieve the current user.

3. Injecting into Routes:

While this solves the problem of accessing context-aware data within templates, we still need to inject the UserContext bean into our routes to make it available to Qute.

@GET
@Path("/profile")
public Uni<Response> profile(@Inject UserContext userContext) {
    return Uni.createFrom().item(Response.ok(templates.render("profile", userContext)));
}

In this example, we inject the UserContext into our route and pass it to the Qute renderer. This ensures that the template has access to the context-aware data during rendering.

Advantages of Using a CDI Bean

  • Dynamic Data: CDI allows us to inject context-aware beans, ensuring that the data within our Qute templates is dynamic and reflects the current user or other request-specific information.
  • Decoupling: By using a separate bean for managing common state, we achieve a cleaner separation of concerns. Our routes and templates focus on their respective responsibilities, while the common state management is handled by the dedicated CDI bean.
  • Testability: CDI beans are easily testable, allowing us to mock and manipulate the data they provide for unit testing purposes.

Conclusion

Injecting context-aware common state into Quarkus Qute templates is essential for building truly dynamic web applications. By leveraging CDI beans and using injection within both our routes and templates, we can seamlessly access context-dependent data while maintaining a clean and organized architecture.

Remember, this approach allows us to build robust and scalable applications while keeping our Qute templates free from complex injections and boilerplate code.

References:

  1. Stack Overflow question: "Inject context-aware common state into Quarkus Qute template?"