srini
srini

Reputation: 6969

how to use macro 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

Upvotes: 0

Views: 6801

Answers (2)

Vijay Dwivedi
Vijay Dwivedi

Reputation: 1

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&amp;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

Upvotes: 0

ddekany
ddekany

Reputation: 31152

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.

Upvotes: 4

Related Questions