簡體   English   中英

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>';

編輯:根據我發布此答案后發現的新信息-發生您的問題是因為Moustache要求將數據存儲在關聯數組中。

// 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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM