We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 27106
    • 147 Posts
    David Walker Reply #1, 12 years ago
    I have a snippet that transforms a named XML file into HTML, as follows:
    <?php
    if (file_exists('transfer/xml/MyXMLFile.xml'))
    	{
        $xml = simplexml_load_file('transfer/xml/MyXMLFile.xml');
    //START MAIN OUTPUT
    	echo "<h4>" . $xml->DigestName->Country . " - " . $xml->DigestName->Category . "</h4>";
    	echo "<h3>" . $xml->DigestName->Headline . "</h3>";
    //xpath to loop over P tags inside Abstract
    	foreach ($xml->xpath('//Abstract/P') as $P)
    		{
    		echo "<p>" . $P . "</p>";
    		}
    //END MAIN OUTPUT
    	} 
    else
    	{
        exit('Could not open XML file.');
    	}


    Now I'd like to be able to pass to the snippet the value of the filename. Ideally I'd also like to specify the HTML tags in a template as well, and pass the template name to the snippet as well. This is beyond my own abilities; can anyone point to existing code that does this, OR give me an idea of how much work it would be for someone to do this.
      David Walker
      Principal, Shorewalker DMS
      Phone: 03 8899 7790
      Mobile: 0407 133 020
      • 3749
      • 24,544 Posts
      Hi David,

      Passing the filename and Templatename is pretty simple:

      [[!SnippetName? &fileName=`somefilename` &xmlTpl=`someTemplatename`]]


      Then in the snippet:

      $fileName = $modx->getOption('fileName', $scriptProperties, '');
      if (empty($fileName)) {
         return 'No Filename');
      }
      
      $templateName = $modx->getOption('xmlTpl', $scriptProperties, '');
      if (empty($templateName)) {
         return 'No Templatename');
      }
      


      Using a Tpl chunk for the output is also fairly simple, though setting the fields from the XML results may be tedious, depending on how big the XML file is.

      Basically, you create a chunk (lets call it xmlTpl) using placeholders for each entry:

      <h4>Country: [[+country]]</h4>
      <h3>[[+headline]]</h3>
      

      Then create an array of fields with the same names from the XML results:

      $fields = array(
          'country' => $xml->DigestName->Category,
          'headline' => $xml->DigestName->Headline,
          // etc
      );
      


      Be careful to use => rather than = in the array and end each line with a comma, not a semicolon.

      Once that's done, you just add this line:

      return $modx->getChunk('xmlTpl', $fields);


      There is a way to generate the fields array automatically from the XML result (assuming that the fields are in one dimension -- i.e., not nested), but if the XML file is short and doesn't change often, it may not be worth the trouble.


        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