Mustache.php渲染多维数据

Mustache.php rendering multi-dimensional data

我正在利用 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>';

编辑: 根据我发布此答案后发现的新信息 - 出现您的问题是因为 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
                );

终于修好了。数据格式不正确:

数据:

$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
            }
        }
    );

代码:

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