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

    I've been learning during the last weeks on how to use modx by building a test website and find it very interesting.

    I have a debate goind in my mind right now about chunk templating and would like some input from other experienced modx users.

    I am trying to decide which way is more efficient to handle dynamic content:

    1. Use chunks and templating. The dynamic HTML is generated by a snippet.
    2. Use chunks with javascript. The dynamic HTML is generated by a javascript function.

    For example, to create a dynamic table. I could:

    1. Create a chunk for the rows. Then in a php script (snippet) get the data and generate the output.
    2. Get the data from the database, send it to the chunk and have a javascript function generate the table.

    The reason why I'm not conviced with the first method is that I would have to create many chunks. With the javascript method I only have 1 chunk. Also, I think that the second method makes things easier for AJAX.

    Am I correct in my thinking? What do you people think?

    Thank you
    • If your table doesn't have thousands of rows, I'd rather using snippet.

      If it does, I'd rather using AJAX using REST method (depends on the JS framework, the REST would work for the scrolling or pagination).
      This option also skips chunk, you could depend on the JS's templating system.
        Rico
        Genius is one percent inspiration and ninety-nine percent perspiration. Thomas A. Edison
        MODx is great, but knowing how to use it well makes it perfect!

        www.virtudraft.com

        Security, security, security! | Indonesian MODx Forum | MODx Revo's cheatsheets | MODx Evo's cheatsheets

        Author of Easy 2 Gallery 1.4.x, PHPTidy, spieFeed, FileDownload R, Upload To Users CMP, Inherit Template TV, LexRating, ExerPlan, Lingua, virtuNewsletter, Grid Class Key, SmartTag, prevNext

        Maintainter/contributor of Babel

        Because it's hard to follow all topics on the forum, PING ME ON TWITTER @_goldsky if you need my help.
        • 3749
        • 24,544 Posts
        JavaScript can be pretty slow. Usually, the fastest method is to use a chunk with placeholder tags. You put the values for the placeholders into an array, then use $modx->getChunk() and return the result.

        $fields = array(
            'placeholder1' => $value1,
            'placeholder2' => $value2,
            
        );
        return = $modx->getChunk('ChunkName', $fields);
        


        You can make it a little bit faster by using str_replace() to replace the placeholders, but the method above is usually fast enough.
          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
          • 39194
          • 149 Posts
          Don't forget about search engines and noscript users, they don't like javascript-only (without noscript fallback) websites ))
            • 42467
            • 8 Posts
            Thanks for the input.

            It seems that chunks is the way to go. There is one small issue that still bothers me when using chunks. I'm trying to use them in the most pure way, taking out all html from the snippets. But when using a <table> I cannot do this totally.

            I would need to have in my snippet something like:

            $output = $modx->getChunk('ChunkName', $fields);
            return '<table><tbody>'.$output.'</tbody></table>'

            The only way to remove this html would be to create 2 more chunks, one for "table start" and another for "table end".

            Is there a better way to do this?
              • 4172
              • 5,888 Posts
              see this one from today:

              http://forums.modx.com/thread/81512/separating-html-from-snippet#dis-post-449576

              or use bloX for rendering nested chunks
                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 3749
                • 24,544 Posts
                The usual solution is to have two or three Tpl Chunks. This example is from my book. It shows one way to do it with a list, but I think you'll get the idea:


                <!--ItemTpl Code -->
                <span class= "sl.item_class">[[+sl.item]]</span>
                


                <!--RowTpl Code -->
                <li class="sl.row_class">[[+sl.row]]</li>



                <!-- OuterTpl Code -->
                <div class="sl.show_list ">
                   <ul class="sl.outer_class">
                   [[+sl.outer]]
                </ul>
                </div>

                  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