在另一个FTL文件中导入一个FTL文件

时间:2011-05-18 04:55:28

标签: java spring-mvc freemarker

我在FTL文件中创建了一个DIV,DIV包含表单现在说我有另一个FTL文件,我想在第二个FTL文件中使用第一个FTL的div这是可能的

deepak.ftl

<div id="filterReportParameters" style="display:none">
    <form method="POST" action="${rc.getContextPath()}/leave/generateEmpLeaveReport.json" target="_blank">
    <table border="0px" class="gtsjq-master-table">
        <tr>
            <td>From</td>
            <input type="hidden" name="empId" id="empId"/>
            <td>
            <input type="text" id="fromDate" name="fromDate" class="text ui-widget-content ui-corner-all" style="height:20px;width:145px;"/>
            </td>
            <td>Order By</td>
            <td>
                <select name="orderBy" id="orderBy">
                    <option value="0">- - - - - - - Select- - - - - - - -</option>
                    <option value="1">Date</option>
                    <option value="2">Leave Type</option>
                    <option value="3">Transaction Type</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>To</td>
            <td><input type="text" id="toDate" name="toDate" class="text ui-widget-content ui-corner-all" style="height:20px;width:145px;"/>
        </tr>
        <tr>
            <td>Leave Type</td>
            <td>
                <select name="leaveType" id="leaveType">
                    <option value="0">- - - - - - - Select- - - - - - - -</option>
                    <#list leaveType as type>
                        <option value="${type.id}">${type.leaveType.description}</option>
                    </#list> 

                </select>
            </td>

        </tr>
        <tr>
            <td>Leave Transaction</td>
            <td>
                <select name="transactionType" id="transactionType">
                    <option value="0">- - - - - - - Select- - - - - - - -</option>
                    <#list leaveTransactionType as leaveTransaction>
                        <option value="${leaveTransaction.id}">${leaveTransaction.description}</option>
                    </#list> 

                </select>
            </td>
        </tr>
    </table>
    </form>

如何在另一个FTL文件中使用此div

2 个答案:

答案 0 :(得分:12)

如果您只想将一个自由标记模板中的div包含在另一个自由标记模板中,则可以通过using a macro提取公共div。例如,

in macros.ftl:
<#macro filterReportDiv>
    <div id="filterReportParameters" style="display:none">
      <form ...>
    ..
      </form>
    </div>
 </#macro>

然后在两个freemarker模板中,您可以导入macros.ftl并通过以下内容调用宏:

<#import "/path/to/macros.ftl" as m>
<@m.filterReportDiv /> 

宏是FreeMarker中的一个很棒的功能,也可以参数化 - 它们可以真正减少模板中的代码重复。

答案 1 :(得分:11)

听起来你正在寻找<#include>指令 - 包含的文件将由Freemarker处理,就像它是包含文件的一部分一样。

如果两个FTL文件位于同一目录中,

<#include "deepak.ftl">将起作用。如果不是,你可以使用相对路径。

相关问题