phpmustachemustache.php

Mustache.php rendering multi-dimensional data


I'm utilizing Mustache to template some XML responses for an API. I was wondering how I could use the XML template below to render data from this array? The data is not rendering at all when using this code:

$result = $m->render($template, $r);             
echo $result;

Here is JSON converted data:

[
    {
        "UUID": "655482ab-38ee-433f-b310-1f6f227113b9",
        "RefUUID": "92a8ecf6-8eb6-4d1e-b88d-59b50c3b0cc9",
        "company":"UAR",
        "itemname":"DOOR ",
        "daysinstock":"41",
        "condition":"A",
        "stocknumber":"F0049356",
        "ic":"120-00409AL",
        "price":"750.00",
        "quantity":"1",
        "location":"U3020",
        "comments": "comment for #0"
    },
    {
        "UUID": "655482ab-38ee-433f-b310-1f6f227113b9",
        "RefUUID": "92a8ecf6-8eb6-4d1e-b88d-59b50c3b0cc9",
        "company":"UAR",
        "itemname":"DOOR ",
        "daysinstock":"68",
        "condition":"C",
        "stocknumber":"F0048586",
        "ic":"120-00409AL",
        "price":"750.00",
        "quantity":"1",
        "location":"KEEP"
        "comments": "comment for #1"
    },
    {
        "UUID": "655482ab-38ee-433f-b310-1f6f227113b9",
        "RefUUID": "92a8ecf6-8eb6-4d1e-b88d-59b50c3b0cc9",
        "company":"UAR",
        "itemname":"DOOR ",
        "daysinstock":"280",
        "condition":"B",
        "stocknumber":"171013",
        "ic":"120-00409AL",
        "price":"750.00",
        "quantity":"1",
        "location":"YCR4"
        "comments": "comment for #2"
    }
]

XML template trying to render

$template = '<SupplierResponse>
    <QuotedPartList>
        {{#parts}}
        <QuotedPart>
            <BMSObject>
                <UUID>{{UUID}}</UUID>
                <RefUUID>{{RefUUID}}</RefUUID>
            </BMSObject>
            <SupplierResponseCode>AsRequested</SupplierResponseCode>
            <SupplierRefLineNum>{{SupplierRefLineNum}}</SupplierRefLineNum> 
            <PartNumInfo>
                <PartNumType>Stock</PartNumType>
                <PartNum>{{stocknumber}}</PartNum>
            </PartNumInfo>
            <PartNumInfo>
                <PartNumType>IC</PartNumType>
                <PartNum>{{ic}}</PartNum>
            </PartNumInfo>
            <PartType>PAL</PartType>
            <PartDesc>{{itemname}}</PartDesc>
            <PriceInfo>
                <UnitListPrice>{{price}}</UnitListPrice>
                <UnitNetPrice>{{price}}</UnitNetPrice>
            </PriceInfo>
            <RInfo>
                <Grade>{{condition}}</Grade>
                <DaysInStock>{{daysinstock}}</DaysInStock>
                <PartLocation>{{location}}</PartLocation>
                <PartStore>{{company}}</PartStore>
            </RInfo>
            <Availability>
                <Quantity>{{quantity}}</Quantity>
                <InventoryStatus>Available</InventoryStatus>
                <AvailableShipDate>2018-05-10</AvailableShipDate>
            </Availability>
            <LineNoteInfo>
                <LineNoteMemo>{{comments}}</LineNoteMemo>
            </LineNoteInfo>
        </QuotedPart>
        {{/parts}}
    </QuotedPartList>
</SupplierResponse>';

Solution

  • Finally got it fixed. The data was not formatted correctly:

    Data:

    $r = array("parts"=> array(
                    "UUID"=> "655482ab-38ee-433f-b310-1f6f227113b9",
                    "RefUUID"=> "92a8ecf6-8eb6-4d1e-b88d-59b50c3b0cc9",
                    "company"=>"UAR",
                    "itemname"=>"DOOR ",
                    "daysinstock"=>"41",
                    "condition"=>"A",
                    "stocknumber"=>"F0049356",
                    "ic"=>"120-00409AL",
                    "price"=>"750.00",
                    "quantity"=>"1",
                    "location"=>"U3020",
                    "comments"=> "comment for #0",
                    "SupplierRefNum"=> 1
                ),
                array(
                    "UUID"=> "655482ab-38ee-433f-b310-1f6f227113b9",
                    "RefUUID"=> "92a8ecf6-8eb6-4d1e-b88d-59b50c3b0cc9",
                    "company"=>"UAR",
                    "itemname"=>"DOOR ",
                    "daysinstock"=>"68",
                    "condition"=>"C",
                    "stocknumber"=>"F0048586",
                    "ic"=>"120-00409AL",
                    "price"=>"750.00",
                    "quantity"=>"1",
                    "location"=>"KEEP",
                    "comments"=> "comment for #1",
                    "SupplierRefNum"=> 2
                ),
                array(
                    "UUID"=> "655482ab-38ee-433f-b310-1f6f227113b9",
                    "RefUUID"=> "92a8ecf6-8eb6-4d1e-b88d-59b50c3b0cc9",
                    "company"=>"UAR",
                    "itemname"=>"DOOR ",
                    "daysinstock"=>"280",
                    "condition"=>"B",
                    "stocknumber"=>"171013",
                    "ic"=>"120-00409AL",
                    "price"=>"750.00",
                    "quantity"=>"1",
                    "location"=>"YCR4",
                    "comments"=> "comment for #2",
                    "SupplierRefNum"=> 3
                }
            }
        );
    

    Code:

    $result = $m->render($template, $r); // Used same template as in my original post.