Jmini
Jmini

Reputation: 9497

Insert a java string constant in a quarkus qute template?

I am working on a small quarkus project serving HTML pages created with the qute templating engine.

I was wondering if it possible to access some String constant values to the template, without having to pass them as .data("key", value) to the template.

One example, I have defined constants for my query parameters, and I would like to use them in the HTML I am producing with the template engine.

Adapted from the official guide Qute is a templating engine

My JAX-RS class /src/main/java/com/company/HelloResource.java looks like this:

package com.company

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import io.quarkus.qute.Template;
import io.quarkus.qute.TemplateInstance;

@Path("hello.html")
public class HelloResource {

    @Inject
    Template hello;

    private static final String NAME_QUERY_PARAM = "name";

    @GET
    @Produces(MediaType.TEXT_HTML)
    public TemplateInstance get(@QueryParam(NAME_QUERY_PARAM) String name) {
        String helloStatement;
        if (name == null) {
            helloStatement = "Welcome!";
        } else {
            helloStatement = "Hello " + name + "!";
        }
        return hello.data("helloStatement", helloStatement);
    }
}

And the hello template /src/main/resources/templates/hello.html looks like this:

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Test</title>
</head>
<body>

<h1>{helloStatement}</h1>

<form action="./hello.html" method="GET">
  <div>
    <label for="name">The name:</label>
    <input name="name" id="name" value="John">
  </div>
  <div>
    <button>Say hi!</button>
  </div>
</form>

</body>
</html> 

For the <input> tag I would like to write:

 <input name="{com.company.HelloResource.NAME_QUERY_PARAM}" id="name" value="John">

And having com.company.HelloResource.NAME_QUERY_PARAM replaced with its value at compile time.

Upvotes: 2

Views: 1644

Answers (4)

Jmini
Jmini

Reputation: 9497

As indicated by Martin Kouba a way to solve this is to use the TemplateData annotation:

package com.company;

import io.quarkus.qute.TemplateData;

@TemplateData 
public class Constants {
    public static final String NAME_QUERY_PARAM = "name";
}

And use it like this:

<input name="{com_company_Constants:NAME_QUERY_PARAM}" id="name" value="John">

See Accessing Static Fields and Methods.

Upvotes: 1

Martin Kouba
Martin Kouba

Reputation: 1526

There is no built-in way to access static fields directly in Qute yet. Begui and özkan are right: you can either use an extension method with a namespace or inject a @Named bean.

Upvotes: 0

Begui
Begui

Reputation: 2856

Seems like you can inject CDI components directly into qute.. I created a @ApplicationScoped @Named bean

@ApplicationScoped
@Named 
public class ApplicationBean {
    @ConfigProperty(name = "quarkus.application.name")
    String application;
    
    public String getName() {
        return application;
    }
}

and reference in my template like so

 <title>{inject:applicationBean.name}</title>

Upvotes: 1

ozkanpakdil
ozkanpakdil

Reputation: 4612

Closest you can get "without passing the values" is using @TemplateExtension you can see more details about it at https://quarkus.io/guides/qute-reference#template_extension_methods

I prepared a small example to show quarkus way to reach constant string. Below how html looks like

    <h1>{item.someVar}</h1>
    <h1>{str:Any1}</h1>
    <h1>{str:g('2')}</h1>
    <h1>{str:g('1')}</h1>

"str" is the namespace approach of quarkus and for "str" to work here is the needed code.

You can also put the constant to in a class next to object used for templates, here how.

And here how the example looks in my local. enter image description here

Upvotes: 0

Related Questions