1

现在我有一个base.leaf文件可以成功地从其他文件中导入正文。

/// base.leaf
<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    .
    .
    .
    <!-- Begin page content -->
    <div class="body-content">
     #import("content")
    </div>
    .
    .
    .
  </body>
</html>

在我的report.leaf文件中,我需要根据所选选项在此页面底部显示不同的报告模板。例如,如果Wire选择了,我想从wire.leaf文件中导入那部分代码,依此类推。在 GRAILS GROOVY 中,部分文件的导入是通过<g:render template="/shared/report/wire" />. 但我似乎无法弄清楚如何在vapor/leaf.

/// report.leaf
#extend("base")

#export("content") {
  <h2>Generate Report</h2>
  <section>
    <ul>
      <li>
        <label for="report">select report</label>
        <select name="report">
          <option value="-1">-- Select Report --</option>
          <option value="1">Purchaser Confirm</option>
          <option value="2">Wire</option>
          <option value="3">Withdrawal Letter</option>
        </select>
      </li>
      <li>
       <input type="submit" value="run  report" />
      </li>
    </ul>
  </section>

  /// Display different report templates based on the selected option
  <!-- #export("report") { #embed("section") } -->
  <!-- #import("wire") -->
  <!-- #embed("section") -->
  <!-- #import("report-content") -->
} 

这是我的wire.leaf文件。

/// wire.leaf
<!-- 
/// Trying the:  #export("report") { #embed("section") } 
<section>
  <h3>Wire info for Loan # 123456789</h3>
  <div>
    <ul>
      <li>Name: Marlin Bank</li>
      <li>CMG: 007</li>
      <li>MtDt: 005689</li>
      <li>CUSIP: BDTK001</li>
      <li>GP: 5</li>
    </ul>
  </div>
  <div>
    <input type="submit" value="print" />
  </div>
</section> 
-->

/// Trying the:  #import("report-content")
#export("report-content") {
<section>
  <h3>Wire info for Loan # 123456789</h3>
  <div>
    <ul>
      <li>Name: Marlin Bank</li>
      <li>CMG: 007</li>
      <li>MtDt: 005689</li>
      <li>CUSIP: BDTK001</li>
      <li>GP: 5</li>
    </ul>
  </div>
  <div>
    <input type="submit" value="print" />
  </div>
</section>
}

我确实阅读了这个文档,#embed但我仍然很困惑。任何帮助将不胜感激!

4

1 回答 1

1

Vapor 在服务器端运行。这意味着它自己不会知道在呈现模板时客户端选择了哪个选项。当用户可以看到页面并与之交互时,Vapor 不再参与。

这意味着您有两个选择。要么使用客户端编程(即 JavaScript,或其众多框架之一)在用户选择模板时显示正确的模板,要么使客户端选择选项的动作强制从 Vapor 服务器重新加载,这将现在知道制作什么模板。

JavaScript 选项:您将在生成的 HTML 中包含所有选项的 HTML 代码,为每个选项display: none设置类似的内容,并在框上添加一个侦听器,该侦听器根据select需要动态显示和隐藏内容;或者,使用Vue.js 之类的东西为您处理模板,甚至可能完全绕过 Leaf。

服务器端选项:您应该在select盒子上收听。用户选择选项的行为应该会导致窗口导航到类似/report/?option=wire. Vapor 应该寻找GET名为 的变量option,如果存在,则渲染适当的模板部分。

(混合选项,为了完整性:当用户选择一个选项时,JS 向 Vapor 发送请求,仅获取内容部分并将其插入到文档中。)

于 2018-01-22T00:53:26.043 回答