带有脚本内联的Vapor 3渲染叶子模板

问题描述 投票:1回答:1

我正在尝试使用Leaf在Vapor 3中渲染模板。我的大多数HTML都在我的base.leaf中。在login.leaf模板中,我需要添加一个JS脚本。问题是当它到达函数时会中断并呈现该函数。谁能告诉我如何正确添加这些内容?感谢您的帮助。这就是给我带来问题的原因:

#set("content") {
  <h1>#(title)</h1>
<div id="logo"><img src="images/global/journey_trax_logo.png" alt=""/></div>
<div id="sheet1" class="form_sheet">
    <input type="text" class="text_input" id="name_field" placeholder="NAME">
    <input type="password" class="text_input" id="password_field" placeholder="PASSWORD">
    <div id="continue_btn1" class="text_btn" onClick="logIn();">Continue</div>
    <p>&nbsp;</p>
</div>
<script>
    var user_name = readCookie("user_name");
    var user_password = readCookie("user_password");
    document.getElementById("user_name").value = user_name;
    document.getElementById("user_password").value = user_password;

    function logIn() {
        var baseURL = window.top.location.href;
        if (baseURL.indexOf("#") != -1) {
            baseURL = window.location.href.split("#")[0];
        }
        var url = baseURL + "login";
        console.log("[logIn] post to URL: "+url);
        console.log("[logIn] name: "+user_name+", password: "+user_password);
        $.post( url,
          {
              name: user_name,
              password: user_password
          },
          function(data) {
            // Do something with the return data?
            console.log("[logIn] callback data: "+data);
            console.log("[logIn] handle succes or error");
            //
            parent.loadHTML('screens/home.html');
          }
       );
    }
</script>
    <style>
        .text_input, select {
            width: 100%;
            margin-bottom: 30px;
        }

        .text_btn {
            width: 100%;
            margin-top: 20px;
            cursor: pointer;
        }
    </style>
}
#embed("base")
vapor leaf
1个回答
1
投票

您的基本问题是}捕获了Javascript中的Leaf字符,因为它位于#set标记内。您有两种选择:

  1. 将其保留在}标记内的所有<script>实例转义为\}的位置。据我所知,它不需要以相同的方式转义{。据推测,这是因为之前没有Leaf标签。这可靠地工作,您可以使用Leaf在将其发送到客户端之前生成JavaScript服务器端。

  2. <script>和内容移至#set标签上方(即,位于外部)。如果您确实在javascript中嵌入了Leaf标记,则需要开始转义属于Javascript的所有}字符作为选项1。

    ]
© www.soinside.com 2019 - 2024. All rights reserved.