如何在Qute Template Engine中注入JS脚本
How to inject a JS script in Qute Template Engine
我正在使用 Quarkus
和 qute
模板引擎。我需要注入一些动态 js
脚本以在 HTML 页面上加载。但是 qute
像这样转换 js
:
模板文件hello.html
:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>redirect by script</title>
</head>
<body>
<p>Hi {name}!</p>
<script>
{script}
</script>
</body>
</html>
控制器
@RequestScoped
public class Resource {
@Inject
@Location("hello.html")
Template hello;
@Route(path = "/s/:name", methods = HttpMethod.GET)
public Uni<String> rScript(RoutingContext rc) {
String s = "console.log('Hi from script');";
return Uni.createFrom().completionStage(() -> hello
.data("name", rc.request().getParam("name"))
.data("script", s)
.renderAsync());
}
}
如下所示的模板渲染文件和脚本不会在浏览器上运行:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>redirect by script</title>
</head>
<body>
<p>NOT_FOUND!</p>
<script>
console.log('test redirect by script')
</script>
</body>
</html>
如何将脚本数据传递到 qute
模板文件?
您将要关闭 Character Escapes:
要么使用 raw
:
<script>
{script.raw}
</script>
</body>
</html>
或return一个RawString
:
@Route(path = "/s/:name", methods = HttpMethod.GET)
public Uni<RawString> rScript(RoutingContext rc) {
String s = "console.log('Hi from script');";
return Uni.createFrom().completionStage(() -> hello
.data("name", rc.request().getParam("name"))
.data("script", new RawString(s))
.renderAsync());
}
}
我正在使用 Quarkus
和 qute
模板引擎。我需要注入一些动态 js
脚本以在 HTML 页面上加载。但是 qute
像这样转换 js
:
模板文件hello.html
:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>redirect by script</title>
</head>
<body>
<p>Hi {name}!</p>
<script>
{script}
</script>
</body>
</html>
控制器
@RequestScoped
public class Resource {
@Inject
@Location("hello.html")
Template hello;
@Route(path = "/s/:name", methods = HttpMethod.GET)
public Uni<String> rScript(RoutingContext rc) {
String s = "console.log('Hi from script');";
return Uni.createFrom().completionStage(() -> hello
.data("name", rc.request().getParam("name"))
.data("script", s)
.renderAsync());
}
}
如下所示的模板渲染文件和脚本不会在浏览器上运行:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>redirect by script</title>
</head>
<body>
<p>NOT_FOUND!</p>
<script>
console.log('test redirect by script')
</script>
</body>
</html>
如何将脚本数据传递到 qute
模板文件?
您将要关闭 Character Escapes:
要么使用 raw
:
<script>
{script.raw}
</script>
</body>
</html>
或return一个RawString
:
@Route(path = "/s/:name", methods = HttpMethod.GET)
public Uni<RawString> rScript(RoutingContext rc) {
String s = "console.log('Hi from script');";
return Uni.createFrom().completionStage(() -> hello
.data("name", rc.request().getParam("name"))
.data("script", new RawString(s))
.renderAsync());
}
}