We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 13312
    • 13 Posts
    Is there a neat way to display the contents of multi-dimensional arrays? Templates (template variables, placeholders) don’t support nested for-loops, nor do I want to transform multi-dimensional array into a single array within a snippet. I hate to think of a single array, containing data mixed with presentation code (html).

    Thanks in advance.
      • 22303 MODX Staff
      • 10,725 Posts
      Quote from: jaanus at Feb 13, 2007, 03:04 PM

      Is there a neat way to display the contents of multi-dimensional arrays? Templates (template variables, placeholders) don’t support nested for-loops, nor do I want to transform multi-dimensional array into a single array within a snippet. I hate to think of a single array, containing data mixed with presentation code (html).

      Thanks in advance.
      It’s quite simple. You write a snippet that loops through your array and you can present it however you want. Typically, I loop through the data sets, create associative arrays of replacement variables at each level/iteration, then use $modx->parseChunk($nameOfChunkVar, $replacementVarArray, ’[+’, ’+]’); or something similar to render each level and/or iteration using these chunks.

      Supporting loops or conditional logic of any kind directly in a template or any other element of content (chunks, TV’s, etc.) would be mixing content and/or style with logic. Logic belongs in scripts, not in content or markup. This really is an important fundamental of the MODx way of doing things IMHO.

      However, if you prefer having logical tags in your templates, there is the phX add-on that can do some of the things you want by extending how MODx placeholders work, and the future of MODx will see the ability to extend or override the MODx template parsing engine with Smarty or just about any other engine you might prefer to use. Ultimately, MODx is a content management framework, and though it will continue to provide most of the core services not related directly to content management that it does now, the future will see these services become more easily extensible and interchangeable.
        • 13312
        • 13 Posts
        Thanks, thats exactly what I was looking for smiley