英文:
Insert a java string constant in a quarkus qute template?
问题
我正在进行一个使用 qute 模板引擎创建 HTML 页面的小 quarkus 项目。
我想知道是否可以在模板中访问一些 String 类型的常量值,而不必将它们作为 .data("key", value)
传递给模板。
举个例子,我已经为我的查询参数定义了常量,并且我想在使用模板引擎生成的 HTML 中使用它们。
从官方指南 Qute is a templating engine 进行了适应。
我的 JAX-RS 类 /src/main/java/com/company/HelloResource.java
如下所示:
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);
}
}
而 hello 模板 /src/main/resources/templates/hello.html
如下所示:
<!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>
对于 <input>
标签,我想要写成:
<input name="{com.company.HelloResource.NAME_QUERY_PARAM}" id="name" value="John">
并且在编译时将 com.company.HelloResource.NAME_QUERY_PARAM
替换为其值。
英文:
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.
答案1
得分: 1
似乎您可以直接将CDI组件注入Qute模板中。我创建了一个@ApplicationScoped @Named
的bean:
@ApplicationScoped
@Named
public class ApplicationBean {
@ConfigProperty(name = "quarkus.application.name")
String application;
public String getName() {
return application;
}
}
然后在模板中引用它,如下所示:
<title>{inject:applicationBean.name}</title>
英文:
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>
答案2
得分: 0
最接近"不传递值的情况下"的方法是使用@TemplateExtension
,你可以在https://quarkus.io/guides/qute-reference#template_extension_methods上查看更多详细信息。
我准备了一个小例子,展示了使用Quarkus的方法来获取常量字符串。以下是HTML的样子:
<h1>{item.someVar}</h1>
<h1>{str:Any1}</h1>
<h1>{str:g('2')}</h1>
<h1>{str:g('1')}</h1>
"str"是Quarkus的命名空间方法,为了使"str"在这里起作用,需要使用所需的代码。
你还可以将常量放在与用于模板的对象相邻的类中,如这里所示。
英文:
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.
答案3
得分: 0
目前在 Qute 中没有直接访问静态字段的内置方法。Begui 和 özkan 是对的:你可以使用带有命名空间的扩展方法,或者注入一个 @Named
bean。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论