Gregory
Gregory

Reputation: 79

Templating a Snippet with $modx->getChunk returns nothing

Hi I'm stuck with this output, please suggest what's wrong:

$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>

It returns only the blank chunk template

  • this is is in category pic
  • but when I do the following.. it works

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

    any suggestion... Did I miss something? I want to template my snippet.

    I use: MODX Revolution 2.2.10-pl (advanced) mysql 5.5.37-35.0 PHP Version 5.4.29

    Upvotes: 0

    Views: 1180

    Answers (2)

    Gregory
    Gregory

    Reputation: 79

    I found the solution (SOLVED): I should set the placeholder right before

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

    Or the alternative way is by convert the object variable type into string without setting the placeholder:

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

    Here is the correct answer:

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

    Here is the correct answer 2 (alternative):

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

    Upvotes: 0

    okyanet
    okyanet

    Reputation: 3146

    There's no need to call setPlaceholder, you should be able to pass the placeholders directly to the chunk as you've already attempted.

    I'm not sure why you are setting the $hotelresponse properties into variables, or why you have the properties themselves wrapped in brackets. But your code can be simplified to the following:

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

    I would expect that to work, if the HotelInfo object is valid.

    Upvotes: 1

    Related Questions