0

我正在使用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(&#39;test redirect by script&#39;)
    </script>
    </body>
</html>

如何将脚本数据传递给qute模板文件?

4

1 回答 1

1

您将要关闭Character Escapes

要么使用raw

    <script>
        {script.raw}
    </script>
    </body>
</html>

或返回一个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());
  }
}
于 2021-11-05T13:48:29.190 回答