我们可以从 javascript 函数调用 ftl 宏吗

can we call a ftl macro from javascript function

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title> Java, Spring Boot, FreeMarker</title>
    <link href="/css/main.css" rel="stylesheet">
</head>
<script>
function myFunction() {
        <@test/>
}
</script>
<body>
    <h2>Java, Spring Boot, FreeMarker</h2>
     <form action="/search" method="post">
   Search : <input type="text" name="firstName" onkeyup="myFunction()" id = "fname"> 
   <input type="submit" value="Submit">
   </form>
        <div style="background-color:lightblue">
          <#macro test>
            <#list empList as emp>
            <div id="emp-data">
            <ul>
                <li>${emp}</li>
            </ul>
            </#list>
            </div>
        </#macro>
    <script src="/js/main.js"></script>
    </div>

</body>

当我 运行 此代码时,我在浏览器控制台上收到一些错误:

(index):60 Uncaught ReferenceError: myFunction is not defined at HTMLInputElement.onkeyup ((index):60) onkeyup @ (index):60 – PCS 1 hour ago

在 FreeMarker 中可以做类似的事情吗?

从某种意义上说,您可以...但它并没有像您显然认为的那样工作。首先,所有 FreeMarker 指令,如 <@test/>,都在服务器上解析,然后结果输出在浏览器中运行。所以在浏览器看来,function myFunction() { ... } 直接在 { ... } 里面包含 HTML div-s,这是无效的 JavaScript.