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

    I want to discuss a snippet concept I developed in course of time and get some feedback from the experienced MODx developers.

    Background
    From the beginning of using MODx I didn’t like two things: that everything is "scattered around" many places (documents, snippets, chunks, plugins etc.) and that I had to work mostly in form textareas instead of my beloved TextMate.

    Solution 1: Outsourcing
    So first of all I started to outsource templates and snippets by including them from a file:

    include('assets/site/snippets/metaSnippet.php');


    In this way it was possible again to edit MODx code in Textmate by double clicking a specific file in Transmit ... great!

    Solution 2: Making Collections
    Still I had the problem, that I hat a collection of often used snippets (most of them just a few lines of length and nothing complicated) that where separately located in the snippet area. So I had the idea to collect them all in a snippet called "metaSnippet". Example:

    [[metaSnippet? &name=`getLastEditedDate`]]


    In the metaSnippet I have a PHP switch construction for the different snippets:

    switch($name)
    {	
    	...
    	
    	case "getLastEditedDate":
    		
    		$secs=$modx->documentObject['editedon'];
    		$dateStr=date("Y-m-d, H:i:s (T)", $secs);
    		echo($dateStr);
    		
    	break;
    	
    	...
    }


    In this way I have everything in one place, which I find very handy. Of course for more complex and big snippets I still create individual snippets in MODx.


    I took this concept further for templates too: I am including the template and pass also some parameters down to it:

    [[metaSnippet? &name=`loadTemplate` &template=`screen_default.php` &headChunk=`` &bodyTagParams=`` &contentTopChunk=`` &contentBottomChunk=``]]


    In this way I don’t have to create different copies of the default template if the difference is just one or two lines (like loading an additional style or script).


    I never discussed this concepts of outsourcing and snippet collecting with experienced MODx developers. It works nicely for me and actually I am quite happy with it. But now I just started wondering myself if this ideas are great or there are better and more elegant ways of achieving the same. I am curious for your feedback!

    Thanks

    Bogdan
      Medianotions – Studio für Webdesign
      http://www.medianotions.de
      • 7231
      • 4,205 Posts
      This is a good idea and I don’t see any potential problems with this. I don’t know how the mechanics of the cache are but I am guessing that by loading up all smaller snippets into one multi-purpose snippet may be more efficient...just don’t quote me on this grin

      I like this approach and may be building a multi-purpose snippet for myself on th next project.
        [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

        Something is happening here, but you don't know what it is.
        Do you, Mr. Jones? - [bob dylan]
        • 4971
        • 964 Posts
        I like your idea of a metaSnippet (I would call it megaSnippet smiley) I will try it!
        I probably will think of a metaChunk or something like that too... I use and have way too many chunks...

        I use one template, I include it and then customize everything with chunks and add the chunks and its
        order in a TV. I also include the CSS in a document, so they are external but MODx thinks they are documents
        and then I assign them to each doc.

        Your template idea looks interesting too, I will think about it...

        Good thinking!!
          Website: www.mercologia.com
          MODX Revo Tutorials:  www.modxperience.com

          MODX Professional Partner
          • 25663 MODX Staff
          • 12,272 Posts
          Bogdan, this is a really good technique indeed. In fact we’ve been using exactly the same a similar technique on a large project so that we can get complex snippet logic on the filesystem and managed by subversion. There is tons of stuff you can do with this and hopefully we’ll have some spare cycles to comment further. Suffice it to say it’s a very cool thing. Your implementation is particularly savvy from the fact that it involves Textmate, too! smiley
            Ryan Thrash, MODX Co-Founder
            Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
            • 22303 MODX Staff
            • 10,725 Posts
            I actually use the following to have a single snippet in the database (and only one snippet in the cache file) and this works with just about any snippet (just put the snippet in the php file), unless it relies on a parameter named `file`. If `file` conflicts with something in a snippet you are using with this, you can easily change it to something you know will be a unique variable name:

            <?php
            // &file=`assets/foo/bar.php` indicates the file to include
            // &return=`1` return explicit content from return statement in included file
            //  default behavior is to use output buffering to capture the content from the include
            
            $output = '';
            if (isset($file) && file_exists($file)) {
                if (!isset($return) || $return == false) {
                    ob_start();
                    include ($modx->config['base_path'] . $file);
                    $output = ob_get_contents();
                    ob_end_clean();
                } else {
                    $output = include ($modx->config['base_path'] . $file);
                }
            }
            return $output;
            ?>
              • 3785
              • 143 Posts
              Quote from: OpenGeek at Mar 03, 2008, 09:41 PM

              I actually use the following to have a single snippet in the database (and only one snippet in the cache file) and this works with just about any snippet (just put the snippet in the php file), unless it relies on a parameter named `file`. If `file` conflicts with something in a snippet you are using with this, you can easily change it to something you know will be a unique variable name ...

              Oh ... thats a very good idea. And really simple to implement. Thanks!
                Medianotions – Studio für Webdesign
                http://www.medianotions.de
                • 34017
                • 898 Posts
                Bogdan,

                That does look like a good practice. What are some of the ’cases’ or events you’ve found are good for this (in addition to loadTemplate and getLastEditedDate)?

                Chuck
                  Chuck the Trukk
                  ProWebscape.com :: Nashville-WebDesign.com
                  - - - - - - - -
                  What are TV's? Here's some info below.
                  http://modxcms.com/forums/index.php/topic,21081.msg159009.html#msg1590091
                  http://modxcms.com/forums/index.php/topic,14957.msg97008.html#msg97008
                  • 3785
                  • 143 Posts
                  Hi Chuck,

                  I have a collection of mostly smaller snippets / snippets templates I often use in MODx:

                  • [[redirectLangHome]]
                  • [[redirectParent]]
                  • [[redirect404]]
                  • [[printHeadImage]]
                  • [[loadTemplate]]
                  • [[printFontStyle]]
                  • [[getDocumentDate]]
                  • [[printSidebar]]
                  • [[getTVO]]
                  • [[printDocument]]
                  • [[getDate]]

                  Depending on the requirements of the MODx project I am working on I am adding different Snippets here.
                    Medianotions – Studio für Webdesign
                    http://www.medianotions.de
                    • 22303 MODX Staff
                    • 10,725 Posts
                    The only problem I see with this is that each time a snippet is processed, PHP has to eval all of that code at once. It may not be a significant issue with just a few small scripts, but larger scripts combined like this would definitely reduce the performance of each one being utilized individually.
                      • 3785
                      • 143 Posts
                      That true of course. But compared with the probably thousands of lines of PHP-Code which are processed by MODx when a page is requested my small snippet collection is really light weight wink
                        Medianotions – Studio für Webdesign
                        http://www.medianotions.de