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

    I have a snippet that returns a nested array representing a hierarchy of categories.
    It's said that conceptually a snippet should not return html formated code.
    Is there any preferable way to format an html nested list from the nested array?

    Thanks
      • 27708 MODX Staff
      • 2,502 Posts
      This might give you an idea: http://rtfm.modx.com/display/revolution20/Snippets#Snippets-Don%27ttrytomixPHPandHTMLinaSnippet.

      You could also review getResources snippet code for an example of one that iterates (it's using resources not an array).
        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. Blog ✦ Twitter ✦ LinkedIn ✦ GitHub
        • 3749
        • 24,544 Posts
        The usual method is to create Tpl chunks for each level of the array with placeholders for the data and then use $modx-getChunk() to grab them and build the output.


        ------------------------------------------------------------------------------------------
        PLEASE, PLEASE specify the version of MODX you are using.
        MODX info for everyone: http://bobsguides.com/modx.html
          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
          • 4172
          • 5,888 Posts
          can you show us an example of your array and the wanted output-format?
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 39742
            • 29 Posts
            Hello.
            It is simply a basic hierarchy represented by nested arrays.
            Each node is an array having a "name" string (id is actually the array index) and eventually a "children" array containing children node.
            This is the raw data received by the snippet but I wan't to output it has nested html lists.
            The problem is that html tags must have an equivalent a close tag.
            I would have to create the opening "li" element, then the children inside a "ul" but after I would have to close the element "li".
            I ended up making my snippet return the html lists
            Is the any better way?
            Thanks
              • 3749
              • 24,544 Posts
              Yes, it would definitely work better with Tpl chunks. Say your array looks like this:

              node1
                name
                childen
                    child1
                        childName
                        child1data1
                        child1data2
                  
                    child2
                        childName
                        child2data1
                        child2data2
              
              node2
                 name
                 children
                     child1
                        childName
                         child1data1
                         child1data2
                  
                    child2
                        childName
                        child2data1
                        child2data2


              And you want your output something like this:

              <ul class="MyArray">
              
                <li>NodeName1</li>
                     <ul class="child">
                         <li>Child1Name</li>
                         <ul class="childData">
                               <li>Child1Data1</li>
                               <li>Child1Data2</li>
                         </ul>
                         <ul class="childData">
                               <li>Child1Data1</li>
                               <li>Child1Data2</li>
                         </ul>
                    </ul>
                <li>NodeName2</li>
                     <ul class="child">
                         <li>Child1Name</li>
                         <ul class="childData">
                               <li>Child1Data1</li>
                               <li>Child1Data2</li>
                         </ul>
                         <ul class="childData">
                               <li>Child1Data1</li>
                               <li>Child1Data2</li>
                         </ul>
                      </ul>
              </ul>



              You'd do something like this (this is off the top of my head and almost certainly not correct, but should give you the idea):

              Three chunks:

              OuterTpl:

              <ul class="MyArray">
                  [[+nodes]]
              </ul>



              nodeTpl:
                 <li>[[+nodeName]]</li>
                     [[+children]]



              childTpl:

              <ul class="child">
                <li>[[+childName]]</li>
                    <ul class="childData">
                         [[+childData1]]
                         [[+childData2]]
                   </ul>
              </ul>


              Then in your code:

              <?php
              $outerTpl = $modx->getChunk('outerTpl');
              $nodeTpl = $modx->getChunk('nodeTpl');
              $childTpl = $modx->getChunk('childTpl');
               
              $output = "";
              $nodes = "";
              $children = "";
               
              foreach (MyArrray as $node) {
               
                  $name = $node['name'];
                  $node = str_replace('[[+nodeName]]', $name, $nodeTpl);
                  foreach ($node['children'] as $child) {
                        $child = str_replace('[[+childName]]', $child['name'], $childTpl);
                        $child = str_replace('[[+childData1]]', $child['childData1'], $child);
                        $child = str_replace('[[+childData2]]', $child['childData2'], $child);
                        $children .= $child;
                  }
                 $node = str_replace('[[+children]]', $children, $node);
                 $nodes .= $node;
              }
               
              $output = str_replace('[[+nodes]]', $nodes, $outerTpl);
               
              return $output;


              The advantage of this it that you can then change the output formatting without touching your code.


              ------------------------------------------------------------------------------------------
              PLEASE, PLEASE specify the version of MODX you are using.
              MODX info for everyone: http://bobsguides.com/modx.html [ed. note: BobRay last edited this post 14 years, 3 months ago.]
                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
                • 39742
                • 29 Posts
                Thanks Bob
                Got it.
                Excellent explanation smiley
                  • 3749
                  • 24,544 Posts
                  Glad to help. smiley


                  ------------------------------------------------------------------------------------------
                  PLEASE, PLEASE specify the version of MODX you are using.
                  MODX info for everyone: http://bobsguides.com/modx.html
                    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