使用$ modx-> getChunk模板化片段不会返回任何内容

时间:2014-06-02 09:31:20

标签: php modx modx-revolution

嗨,我坚持这个输出,请说明错误:

$responsexml = @simplexml_load_string($result, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOBLANKS);
foreach ($responsexml as $hotelresponse)
 {
    $hotelname = ($hotelresponse->HotelInfo->Name);
    $hoteldestination = ($hotelresponse->HotelInfo->Destination->Name);
    $hotelcategory = ($hotelresponse->HotelInfo->Category);
    $hotelimage = ($hotelresponse->HotelInfo->ImageList->Image[0]->Url);

    $returnedoutput .= $modx->getChunk('_testxmlchunk',array(
        'hotel' => $hotelname,
        'destination' => $hoteldestination,
        'category' => $hotelcategory,
        'image' => $hotelimage,
    ));
      }

return $returnedoutput;

_testxmlchunk chunk template:

<li>this is <b>[[+hotel]]</b> is in <b>[[+destination]]</b> category <b>[[+category]]</b> pic <img src="[[+image]]"/></li>

它只返回空白块模板

  • 这是 pic
  • 但是当我执行以下操作时......它可以正常工作

    $responsexml = @simplexml_load_string($result, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOBLANKS);
    foreach ($responsexml as $hotelresponse)
     {
        $hotelname = ($hotelresponse->HotelInfo->Name);
        $hoteldestination = ($hotelresponse->HotelInfo->Destination->Name);
        $hotelcategory = ($hotelresponse->HotelInfo->Category);
        $hotelimage = ($hotelresponse->HotelInfo->ImageList->Image[0]->Url);
    
        $returnedoutput .= $hotelname.' '.$hotelimage.' '. $hoteldestination.' '.$hotelcategory.';
         }
    
    return $returnedoutput;
    

    任何建议......我错过了什么吗?我想模拟我的代码段。

    我用: MODX革命2.2.10-pl(高级) mysql 5.5.37-35.0 PHP版本5.4.29

    2 个答案:

    答案 0 :(得分:1)

    无需拨打setPlaceholder,您应该能够将占位符直接传递给您已经尝试过的块。

    我不确定您为什么要将$hotelresponse属性设置为变量,或者为什么将属性本身包含在括号中。但您的代码可以简化为以下内容:

    $returnedoutput = '';
    
    $responsexml = @simplexml_load_string($result, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOBLANKS);
    foreach ($responsexml as $hotelresponse) {
        $returnedoutput .= $modx->getChunk('_testxmlchunk', array(
            'hotel' => $hotelresponse->HotelInfo->Name,
            'destination' => $hotelresponse->HotelInfo->Destination->Name,
            'category' => $hotelresponse->HotelInfo->Category,
            'image' => $hotelresponse->HotelInfo->ImageList->Image[0]->Url,
        ));
    }
    
    return $returnedoutput;
    

    如果HotelInfo对象有效,我希望能够工作。

    答案 1 :(得分:0)

    我找到了解决方案(已解决): 我应该在

    之前设置占位符
    $returnedoutput .= $modx->getChunk('_testxmlchunk',array(
            'hotel' => $hotelname,
            'destination' => $hoteldestination,
            'category' => $hotelcategory,
            'image' => $hotelimage,
        ));
    

    或者另一种方法是将对象变量类型转换为字符串而不设置占位符:

    $returnedoutput .= $modx->getChunk('_testxmlchunk',array(
    'hotel' => (string)$hotelname,
    'destination' => (string)$hoteldestination,
    'category' => (string)$hotelcategory,
    'image' => (string)$hotelimage,
    ));
    

    这是正确答案

    $responsexml = @simplexml_load_string($result, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOBLANKS);
    foreach ($responsexml as $hotelresponse)
     {
        $hotelname = ($hotelresponse->HotelInfo->Name);
        $hoteldestination = ($hotelresponse->HotelInfo->Destination->Name);
        $hotelcategory = ($hotelresponse->HotelInfo->Category);
        $hotelimage = ($hotelresponse->HotelInfo->ImageList->Image[0]->Url);
    
    
        $modx->setPlaceholder('hotel',$hotelname);
        $modx->setPlaceholder('destination',$hoteldestination);
        $modx->setPlaceholder('category',$hotelcategory);
        $modx->setPlaceholder('image',$hotelimage);
    
        $returnedoutput .= $modx->getChunk('_testxmlchunk',array(
            'hotel' => $hotelname,
            'destination' => $hoteldestination,
            'category' => $hotelcategory,
            'image' => $hotelimage,
        ));
    
    
      }
    return $returnedoutput;
    

    这是正确答案2(替代方案)

    $responsexml = @simplexml_load_string($result, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOBLANKS);
    foreach ($responsexml as $hotelresponse)
     {
        $hotelname = ($hotelresponse->HotelInfo->Name);
        $hoteldestination = ($hotelresponse->HotelInfo->Destination->Name);
        $hotelcategory = ($hotelresponse->HotelInfo->Category);
        $hotelimage = ($hotelresponse->HotelInfo->ImageList->Image[0]->Url);
    
                /***
                 cast the xml object into string, $hotelname variable type is object, you should convert it to string
                ***/
        $returnedoutput .= $modx->getChunk('_testxmlchunk',array(
            'hotel' => (string)$hotelname,
            'destination' => (string)$hoteldestination,
            'category' => (string)$hotelcategory,
            'image' => (string)$hotelimage,
        ));
    
    
      }
    return $returnedoutput;