Mustache.php渲染多维数据

时间:2018-05-09 22:05:46

标签: php mustache mustache.php

我正在利用Mustache来模拟API的一些XML响应。我想知道如何使用下面的XML模板从这个数组中呈现数据? 使用此代码时,数据根本无法渲染:

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

以下是JSON转换数据:

[
    {
        "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 = '<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>';

1 个答案:

答案 0 :(得分:1)

编辑:根据我发布此答案后发现的新信息 - 您的问题发生了,因为Mustache要求数据存储在关联数组中。

// Not correct
$data = [
    [
        'Foo' => 'Bar'
    ],
    [
        'Biz' => 'Buz'
    ],        
]

// Correct
$data = [
    'MyData' => [
        [
            'Foo' => 'Bar'
        ],
        [
            'Biz' => 'Buz'
        ]
    ]        
]

您可以尝试这样的事情:

<?php

$objectToPassIn = [
    'parts' => [
        // .. your data here
    ]
];

// Load template and initialize Mustache
$m = new Mustache_Engine(array(
                'loader' => new Mustache_Loader_FilesystemLoader('path/to/where/template/is/stored', array('extension' => '.xml'))
            ));

$rendered = $m->render(
                    'template-name-without-file-extension',
                    $objectToPassIn
                );