We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 13735
    • 62 Posts
    Hello there,

    well, I ran into some issues using $modx->getChunk and I’m using beta 5.

    [[snippet? &tpl=`existingChunk`]]
    $tpl = $modx->getChunk($tpl);
    
    [...] define $params [...]
    
    return $tpl->process($params);


    Fatal error: Call to a member function process() on a non-object in /path/to/file.php on line 19

    By this actually works:
    $tpl = $modx->getObject('modChunk', array('name' => $tpl));
    
    [...] define $params [...]
    
    return $tpl->process($params);


    So, why is this function mentioned in the manual anyway? xD Or is it supposed to work?

    I’m curious laugh
    JB
      • 22303 MODX Staff
      • 10,725 Posts
      You do not need to call process(); getChunk does not return an object, but the results of processing the chunk using the properties you pass, e.g.
      <?php
      return $modx->getChunk($tpl, $scriptProperties);
      ?>

      IOW it handles getting the modChunk object instance and calling process() for you.
        • 28215
        • 4,149 Posts
        The offending documentation has been fixed.
          shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
          • 13735
          • 62 Posts
          Well that sounds like a great function! Thank you for your help.
            • 10596
            • 18 Posts
            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 doesn't output anything



            but when I do this.. 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.
              • 22303 MODX Staff
              • 10,725 Posts
              You may need to explicitly cast those SimpleXML elements to string when putting them in the array of properties for getChunk, i.e.
                      $returnedoutput .= $modx->getChunk('_testxmlchunk',array(
                          'hotel' => (string)$hotelname,
                          'destination' => (string)$hoteldestination,
                          'category' => (string)$hotelcategory,
                          'image' => (string)$hotelimage,
                      ));
              


              In your second example, you are implicitly doing so by concatenating them with ' '.
                • 10596
                • 18 Posts
                Thanks opengeek that also works too.
                I forgot that since those variables ($hotelname,$hoteldestination,$hotelcategory,$hotelimage) are object, it should be passed as a string.

                Quote from: opengeek at Jun 02, 2014, 06:38 PM
                You may need to explicitly cast those SimpleXML elements to string when putting them in the array of properties for getChunk, i.e.
                        $returnedoutput .= $modx->getChunk('_testxmlchunk',array(
                            'hotel' => (string)$hotelname,
                            'destination' => (string)$hoteldestination,
                            'category' => (string)$hotelcategory,
                            'image' => (string)$hotelimage,
                        ));
                


                In your second example, you are implicitly doing so by concatenating them with ' '.