如何在子模板中包含部分模板

时间:2018-01-19 17:20:41

标签: import vapor leaf

现在我有一个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上阅读了this文档,但我仍然感到非常困惑。任何帮助将受到高度赞赏!

1 个答案:

答案 0 :(得分:1)

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

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

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

服务器端选项:您应该收听select框。用户选择选项的行为应该导致窗口导航到类似/report/?option=wire的内容。 Vapor应该注意名为GET的{​​{1}}变量,如果存在,则渲染相应的模板部分。

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