We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22303 MODX Staff
    • 10,725 Posts
    Quote from: Mark at Aug 18, 2006, 05:52 PM

    Places where multi-part templates are beneficial:

    1. For things with subtemplates like the archive
    2. For things like RSS and JSON which have a header and footer associated with them

    But Mark, what are the actual benefits, as you see it, of having those as multi-part templates in one chunk or a separate file of heredocs? You can easily create these as chunks with names like SnippetNameOne.Header, SnippetNameOne.Body, SnippetNameOne.Footer, or SnippetNameTwo.Container and SnippetNameTwo.SubTplName, and allow users to override those easily in the parameters of your snippets. What about combining templates makes it better than this approach, or put another way, where does this approach fail to address the issues that lead you to decide to use a multi-part template?
      • 18397
      • 3,250 Posts
      Simply put, which is easier for an end user to understand in a single chunk:

      This:

      <h3>Archives</h3>
      <div id="ditto_archivelist">
         <ul id="ditto_ul">
             <ditto:year>
             <li><span class="ditto_year">[+year+]</span>
                 <ditto:month>
                 <li><span class="ditto_month">[+month+]</span>
                     <ul>
                         <ditto:item>
                         <li><a href="[~[+id+]~]">[+title+]</a> (<span class="ditto_date">[+date+]</span>)</li>
                         </ditto:item>
                     </ul>
                 </li>
                 </ditto:month>
             </li>
             </ditto:year>
         </ul>
      </div>
      


      or this:

      <?php
      $archive_pre = <<<TPL
      <h3>Archives</h3>
      <div id="ditto_archivelist">
         <ul id="ditto_ul">
      TPL;
      
      $archive_year_pre = <<<TPL
      <li><span class="ditto_year">[+year+]</span>
      TPL;
      
      $archive_year_month_pre = <<<TPL
      <li><span class="ditto_month">[+month+]</span>
                     <ul>
      
      TPL;
      
      $archive_year_month_item = <<<TPL
                         <li><a href="[~[+id+]~]">[+title+]</a> (<span class="ditto_date">[+date+]</span>)</li>
      EOD:
      
      $archive_year_month_post = <<<TPL
                     </ul>
                 </li>
      TPL;
      
      $archive_year_post = <<<TPL
             </li>
      TPL;
      
      $archive_post = <<<TPL
         </ul>
      </div>
      TPL;
      ?>
      


      EDIT: Typo’s fixed
        • 22303 MODX Staff
        • 10,725 Posts
        Quote from: Mark at Aug 19, 2006, 01:47 AM

        Simply put, which is easier for an end user to understand in a single chunk:

        You are still missing my point. I’m talking about using multiple chunks vs. using any kind of multi-part template. I’m saying it’s easier to edit an individual chunk that is appropriately named, and less taxing at run-time if you don’t combine the parts of a template into a single chunk (or file) at all.
          • 18397
          • 3,250 Posts
          Jason,

          Look at the example. As a user, you have to understand pre and post chunks (8 of them!) versus having one chunk with subtemplates (not a multipart template) for each item that repeats. Yes it is easier to edit multiple chunks for some things but not for subtemplating IMHO.
            • 22303 MODX Staff
            • 10,725 Posts
            Quote from: Mark at Aug 19, 2006, 01:58 PM

            Jason,

            Look at the example. As a user, you have to understand pre and post chunks (8 of them!) versus having one chunk with subtemplates (not a multipart template) for each item that repeats. Yes it is easier to edit multiple chunks for some things but not for subtemplating IMHO.

            I’m still not hearing the benefits, nor buying that it’s harder to understand semantically well named, individual subtemplates, or pre or post templates, or whatever kind of templates. They are still combining logic and presentation, and IMHO, that is not the path of evolution I want to see occur within MODx components. A template is a template is a template, regardless of how many other templates it is embedded in or combined with to produce final output, and as we progress towards a more robust and concise model for 1.0, getting rid of code that uses proprietary methods of templating in key components that are distributed with the framework would be the first step towards making those components reflect the best practice approaches we want to exemplify.

            All that should be involved in subtemplates is a loop in your snippet logic with a set of replacement variables for each iteration, being replaced in that subtemplate. And this code should be relatively trivial, especially once we have recursive parsing performed by the snippet elements themselves (definitely 1.0, possibly 0.9.5 recursive parser, though I’m not as sure about the latter; Raymond?); i.e. once a snippet returns it’s content, that content is then scanned for additional MODx tags, and executes until they are all replaced or a specified maximum number of iterations is met.

            To exemplify this, here is an example of what I mean, where subtemplates are driven by separate snippet calls, providing loose relationships between the elements and complete isolation of logic and presentation at all levels:

            <?php
            // MySnippet snippet
            $content= $modx->getChunk('MySnippet.ContainerTpl');
            $data= some_code_or_function_to_perform_snippet_logic_and_get_data_to_iterate_over();
            $modx->setPlaceholder('MyDataToIterateOver', $data);
            return $content;
            ?>


            <!-- MySnippet.ContainerTpl -->
            <div>
            [[MySnippet.Iterator? &data=`MyDataToIterateOver`]]
            </div>
            


            <?php
            // MySnippet.Iterator snippet
            $output= '';
            if ($collection= $modx->getPlaceholder($data)) {
                foreach ($collection as $item) {
                    $output.= $modx->parseChunk('MySnippet.IteratorTpl', $item, '[+', '+]');
                }
            }
            return $output;
            ?>


            Consider if someone wanted to just change the way the iterator portion worked, they don’t need to touch say the Ditto snippet at all, just modify the DittoIterator (or duplicate, rename it, and modify the chunk/template that calls it) to do what they want, without getting lost in a sea of code in a single snippet or a specific piece of template markup buried in a set of proprietary tags or content splitters.

            I’m not saying I can’t be convinced otherwise, but so far, I’ve still seen no actual benefits to combining templates and subtemplates. In fact, the tendency towards this templating appears to me to just be a side effect of the way the components have been approached thus far in conjunction with some limitations of the current core.
              • 18397
              • 3,250 Posts
              I don’t know how else to say it. If you separate a template that uses subtemplates into many chunks then it becomes confusing. Its like trying to build something if you can only see a 1/4 of the plans at any given time. Its hard. Its much easier when you can see all of the blueprints. So, if you can edit the entire output all at once it makes it much easier IMHO.

              Take Wayfinder for example. Right now it uses a few chunks, infinately better than its predecessor. What if you could template it like this:

              <ul>
              <wayfinder:subnav>
              <li>
              <wayfinder:item>
              <li><a href="[~[+id+]~]">[+title+]</a></li>
              </wayfinder:item>
              <li>
              </wayfinder:subnav>
              </ul>
              


              IMHO, that is much easier. Some people might find it harder. Everyone is different. What about having both ways?

              BOTTOM LINE This is the same idea as Raymond’s new WHILE tags--- a simple foreach. I would love to have that in Ditto but no end user has the new parser yet...

              Edit: Typo’s fixed
                • 25663 MODX Staff
                • 12,272 Posts
                With categories and some semi-intelligent naming of chunks in 0.9.5, the need for a single template file is grossly reduced. An added template parser on top of snippet parsing just seems like extra needless overhead that could be better used elsewhere. I don’t really like the idea of mixing the content with the presentation with the logic, which is what an XML template does in a way. Nor do I like the thought of yet another syntax to have to keep straight.

                I do see merit in a file that is included from the filesystem that has variables assigned for the template, as that’s not really extra overhead, and it allows me to edit from an FTP program in my favorite text editor. Tweaking in Textmate is my preferred method without question, and it would make for a simple way to distribute all the pieces, including CSS or JS as needed, in a single file. Assign the parameter of &includeConfig=`assets/templates/ditto/fancy-new-blog.php` and you’re done. That file might look like:
                 <?php
                //<?php
                /* Mark's super fancy blog template. Builds the following:
                
                <h3>Archives</h3>
                <!-- start main wrapper template -->
                <div id="ditto_archivelist">
                    <ul id="ditto_ul">
                <!-- start repeating year subtemplate -->
                        <li><span class="ditto_year">[+year+]</span>
                <!-- start repeating month subtemplate -->
                            <li><span class="ditto_month">[+month+]</span>
                                <ul>
                <!-- start repeeating item subtemplate -->
                                    <li><a href="[~[+id+]~]">[+title+]</a> (<span class="ditto_date">[+date+]</span>)</li>
                <!--# end item subtemplate -->
                                    </ul>
                            </li>
                <!--# end month subtemplate -->
                        </li>
                <!--# end year subtemplate -->
                    </ul>
                </div>
                <!--# end main tempalte -->
                */
                
                // Template variables below:
                
                // The title
                $dittoTitle = '<h3>[+ditto.title+]</h3>';
                
                // The outer wrapper element for summaries
                $dittoMainWrap = '
                <div id="ditto_archivelist">
                    <ul id="ditto_ul">
                [+ditto.wrapper+]
                    </ul>
                </div>
                ';
                
                // An actual ditto item listing
                $dittoItem = '
                <li><a href="[~[+id+]~]">[+title+]</a> (<span class="ditto_date">[+date+]</span>)</li>
                ';
                
                // CSS styles to include automatically via regClient API
                $dittoCSS = '<style type="text/css">
                h3 { font: bold 16px/16px arial, san-serif }
                ...
                </style>';
                ?>


                This would provide the "30,000 foot view" with the comments above it, added clarity for newbies if you choose to add extra commentary, reduced overhead by eliminating the extra template parsing requirements (smaller snippet and less processor requirements), organization inherent in a single-file implementation, edibility in your favorite text editor, ability to override via chunks in the call, and probably the ability to paste the whole thing into a chunk too if you wanted to pull from the DB vs. the filesystem (&chunkConfig vs. &includeConfig).

                To me, I think this method offers grossly more benefits in the form of clarity, documentation, portability, flexibility and reduced support requirements in the forums (from an increasingly less technical user base), and I suspect less processing overhead. Extras and theme packs become really simple.

                Thoughts?
                  Ryan Thrash, MODX Co-Founder
                  Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
                  • 4018
                  • 1,131 Posts
                  The main problem is indeed the extra overhead applied. Anything dealing with XML can be quite a taxing process...especially if you have to translate the XML content back into standard HTML. But that’s just part of the issue here. The other part is the standardization of how placeholders are used in the code within MODx. I just think that trying to implement a templating system that uses <ditto:item> as its syntax will only confuse current users. Mixing it up rather than standardizing on one technique is not a good idea. I think the standard method of using [+item+] or the like is pretty much already standardized. So there are easier ways to do this without resorting to extra processing on top of what the parser is already doing. The worst thing we can do is create something that won’t work well with large sites. MODx really needs to be built with performance, reliability, and speed in mind. In a nutshell, if it’s easy to understand, performs well, and just works...that’s all anyone asks for. Nuff said. laugh
                    Jeff Whitfield

                    "I like my coffee hot and strong, like I like my women, hot and strong... with a spoon in them."
                    • 18397
                    • 3,250 Posts
                    Ok, I shouldn’t have called this thread XML templating. This thread should have been entitled subtemplatating. I don’t care what the deliminator is between subtemplates.... I just need them so I can separate out the templates. I only used XML as an example because it was easy to parse given it had start and end tags.

                    @Ryan: In your example where are the year and month subtemplates?

                    @Bravado: I too want whatever solution that comes of this to be built with performance, reliability, and speed in mind just as you say. Standardization is important but even with alot of code handling finding and recursively replacing the placeholders the system would still need 4 chunks. Still 3 too many IMHO...
                      • 18397
                      • 3,250 Posts
                      Ryan,

                      Regarding the parameters file, that should be possible in the next release of Ditto.

                      On a related note, in Ditto 1.1 I split off several things such as truncation into classes of their own for maintainability.