在 Quarkus Qute 模板中插入一个 Java 字符串常量?

huangapple go评论62阅读模式
英文:

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(&quot;key&quot;, 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(&quot;hello.html&quot;)
public class HelloResource {

    @Inject
    Template hello;

    private static final String NAME_QUERY_PARAM = &quot;name&quot;;

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

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

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;title&gt;Test&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;h1&gt;{helloStatement}&lt;/h1&gt;

&lt;form action=&quot;./hello.html&quot; method=&quot;GET&quot;&gt;
  &lt;div&gt;
    &lt;label for=&quot;name&quot;&gt;The name:&lt;/label&gt;
    &lt;input name=&quot;name&quot; id=&quot;name&quot; value=&quot;John&quot;&gt;
  &lt;/div&gt;
  &lt;div&gt;
    &lt;button&gt;Say hi!&lt;/button&gt;
  &lt;/div&gt;
&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt; 

For the &lt;input&gt; tag I would like to write:

 &lt;input name=&quot;{com.company.HelloResource.NAME_QUERY_PARAM}&quot; id=&quot;name&quot; value=&quot;John&quot;&gt;

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 = &quot;quarkus.application.name&quot;)
	String application;
	
	public String getName() {
		return application;
	}
}

and reference in my template like so

 &lt;title&gt;{inject:applicationBean.name}&lt;/title&gt;

答案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"在这里起作用,需要使用所需的代码

你还可以将常量放在与用于模板的对象相邻的类中,如这里所示。

这是示例在我的本地环境中的样子。
在 Quarkus Qute 模板中插入一个 Java 字符串常量?

英文:

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

    &lt;h1&gt;{item.someVar}&lt;/h1&gt;
    &lt;h1&gt;{str:Any1}&lt;/h1&gt;
    &lt;h1&gt;{str:g(&#39;2&#39;)}&lt;/h1&gt;
    &lt;h1&gt;{str:g(&#39;1&#39;)}&lt;/h1&gt;

"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.
在 Quarkus Qute 模板中插入一个 Java 字符串常量?

答案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.

huangapple
  • 本文由 发表于 2020年9月25日 13:21:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/64058253.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定