We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 19975
    • 429 Posts
    I have developed a snippet to read my custom xml feed but have issues when trying to return the $output. The script will echo/print successfully but as I am trying to follow MODX's snippet guidelines I would ideally like to return my values.

    Any suggestion very welcomed:

    <?php 
    $XML  = new DOMDocument();
    $XML->loadXML(file_get_contents("path/tp/xml/file.rss"));
    $xpath    = new DOMXPath($XML);
    $property = $xpath->query("//items/*");
    
    foreach ($property as $node) {
    	$child = $xpath->query("//items/$node->nodeName[$iNum]/* ");
        foreach ($child as $node)
        {
        	$output .= $modx->setPlaceholder($node->nodeName, $node->nodeValue);    
      	}
        $output .= $modx->parseChunk((isset($rowTpl)) ? $rowTpl : 'rowTpl', $modx->setPlaceholders, '[[+', ']]');
        $iNum++;
        $output .= $modx->setPlaceholder($node->nodeName, $node->nodeValue);    
        }
    
    return $output;          
    
    ?>
    [ed. note: SandersDesign last edited this post 9 years, 9 months ago.]
      Martin Sanders - Design & Web Development
    • I am no developer but it appears as you've not first defined that $output = ''; and you're jumping right into concatenating values to an undefined variable. What happens if you stick $output = ''; up before your loop?
        Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup magician. BlogTwitterLinkedInGitHub
        • 19975
        • 429 Posts
        Hi Jay,

        The above code was copied without including the defined variables. Well spotted tho smiley The issue seems to be isolated when I include this script as a file within the MODX admin as it works fine when I simply paste the code into the snippet admin.

        I appreciate that this is the way snippets should function so maybe I've just got my wires crossed s: Anyways, if any seasoned MODX PHP developers have advise on how I could improve the above script I'd love to get feedback.
          Martin Sanders - Design & Web Development
        • If it's an external .php file, you need to use echo or print or some other method of getting the output displayed. Snippets inside of MODX use return because they are functions, and MODX takes care of incorporating the returned value into the content to be echo'ed.
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 3749
            • 24,544 Posts
            I think part of your problem is that you're setting the $output to the return value of setPlaceholder() in two places. The setPlaceholder() function doesn't return a value. If you've set a placeholder (and you're displaying the placeholder tag) you don't need to add that to your output.

            FYI, initializing with $output = '' is good form, but leaving it out won't cause trouble unless you have E_NOTICE errors on, in which case it will throw a PHP warning.

              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting
              • 19975
              • 429 Posts
              Thanks for the dev pointers. As the data to be imported already lives on the server and is being converted to rss by my customised third party script templates would pages load quicker if I simply used the "file_get_content" call and imported the HTML code into my MODX templates?

              The code to be imported is generated by phpjabbers.com Vacation Rentals script.

              I know I would effectively bypass chunks and placeholders but as the HTML output could be customised I ultimately have control over how this looks anyways. [ed. note: SandersDesign last edited this post 9 years, 9 months ago.]
                Martin Sanders - Design & Web Development
                • 3749
                • 24,544 Posts
                Yes, I think this would do it:

                return file_get_contents('filename');


                You could add code to set any placeholders above that if you want to.


                Another possibility is to alter the vacation rentals script to have it write the results to a MODX chunk -- tricky, but you'd only have to do it once. Then you could use $modx->getChunk() or just put a chunk tag on the page.


                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting