如何在ftl中使用宏

发布于 2025-01-05 19:44:28 字数 144 浏览 1 评论 0原文

我对ftl中的宏和函数的实现充满了困惑。 任何人都可以添加一些有用的信息。

  and what is the difference between macro and function in ftl

谢谢

I have full of confusion in implementing the macro and functions in ftl.
can any one please add some useful information.

  and what is the difference between macro and function in ftl

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

圈圈圆圆圈圈 2025-01-12 19:44:28

宏和函数之间的区别:宏通常用于生成标记(或其他长文本)以及流程控制和副作用。函数用于计算其他类型的值,包括短的纯文本,并且通常没有副作用。这些反映在宏没有返回值,它们只是直接打印到输出上。另外,宏的输出不会被 #escape 转义。这也是为什么它们看起来与 HTML 标记相似,而 ${myFunction()} 则不然。

除此之外,你还有什么困惑呢?我假设您已经找到了 FreeMarker 手册。

The difference between macros and functions: macros are for generating markup (or other long text) and for flow-control and side-effects in general. Functions are for calculating other kind of values, including short plain text, and usually has no side-effects. These are reflected by that fact that macros has no return value, they just directly print to the output. Also the output of macros is not escaped by #escape. That's also why they look similar to HTML tags, while ${myFunction()} doesn't.

Other than that, what are you confused about? I assume you have found the FreeMarker Manual.

dawn曙光 2025-01-12 19:44:28

下面是如何在 FTL 中使用宏的答案:)

Input-smooks.Json:

 { 
        "title": "Payment Received", 
        "firstName": "vijay", 
        "lastName": "dwivedi", 
        "accountId": "123", 
        "paymentId": "456", 
        "accounts": [ 
            { 
                "accountId": "1111", 
                "paymentId": "1112" 
            }, 
            { 
                "accountId": "2111", 
                "paymentId": "2112" 
            }, 
            { 
                "accountId": "3111",
                "paymentId": "3112" 
            } 
        ] 
    }

Smook-config.xml 文件:

定义一次宏并在需要时用作函数

<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
    xmlns:json="http://www.milyn.org/xsd/smooks/json-1.1.xsd" xmlns:ftl="http://www.milyn.org/xsd/smooks/freemarker-1.1.xsd">

    <params>
        <param name="stream.filter.type">SAX</param>
        <param name="default.serialization.on">false</param>
    </params>

    <json:reader rootName="json" keyWhitspaceReplacement="_">
        <json:keyMap>
            <json:key from="date&time" to="date_and_time" />
        </json:keyMap>
    </json:reader>

    <resource-config selector="json">
        <resource>org.milyn.delivery.DomModelCreator</resource>
    </resource-config>

    <ftl:freemarker applyOnElement="json">
        <ftl:template>
            <!-- 
            <#macro PopulateTasks task_list>
                <#list task_list as att1>
                    "accountId": "${att1.accountId}"
                    "paymentId": "${att1.paymentId}"
                </#list>
            </#macro>

            <@PopulateTasks json.accounts.element/>

             -->
        </ftl:template>
    </ftl:freemarker>

</smooks-resource-list>



  public static void main(String[] args) throws SmooksException, IOException, SAXException {

        long start = System.currentTimeMillis();
        Smooks smooks = new Smooks("src/main/resources/smooks-config.xml");

        try {
            smooks.filterSource(new StreamSource(new 
            FileInputStream("src/main/resources/input_smooks.json")), new 
            StreamResult(System.out));

        } finally {
            smooks.close();
        }
    }

<@PopulateTasks json.accounts.element/>< /code> 这是调用宏的方法

Below is the answer how to use macros in FTL :)

Input-smooks.Json:

 { 
        "title": "Payment Received", 
        "firstName": "vijay", 
        "lastName": "dwivedi", 
        "accountId": "123", 
        "paymentId": "456", 
        "accounts": [ 
            { 
                "accountId": "1111", 
                "paymentId": "1112" 
            }, 
            { 
                "accountId": "2111", 
                "paymentId": "2112" 
            }, 
            { 
                "accountId": "3111",
                "paymentId": "3112" 
            } 
        ] 
    }

Smook-config.xml file:

Define macros once and use as function whenever required

<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
    xmlns:json="http://www.milyn.org/xsd/smooks/json-1.1.xsd" xmlns:ftl="http://www.milyn.org/xsd/smooks/freemarker-1.1.xsd">

    <params>
        <param name="stream.filter.type">SAX</param>
        <param name="default.serialization.on">false</param>
    </params>

    <json:reader rootName="json" keyWhitspaceReplacement="_">
        <json:keyMap>
            <json:key from="date&time" to="date_and_time" />
        </json:keyMap>
    </json:reader>

    <resource-config selector="json">
        <resource>org.milyn.delivery.DomModelCreator</resource>
    </resource-config>

    <ftl:freemarker applyOnElement="json">
        <ftl:template>
            <!-- 
            <#macro PopulateTasks task_list>
                <#list task_list as att1>
                    "accountId": "${att1.accountId}"
                    "paymentId": "${att1.paymentId}"
                </#list>
            </#macro>

            <@PopulateTasks json.accounts.element/>

             -->
        </ftl:template>
    </ftl:freemarker>

</smooks-resource-list>



  public static void main(String[] args) throws SmooksException, IOException, SAXException {

        long start = System.currentTimeMillis();
        Smooks smooks = new Smooks("src/main/resources/smooks-config.xml");

        try {
            smooks.filterSource(new StreamSource(new 
            FileInputStream("src/main/resources/input_smooks.json")), new 
            StreamResult(System.out));

        } finally {
            smooks.close();
        }
    }

<@PopulateTasks json.accounts.element/> this is the way to call macros

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文