We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 39932
    • 483 Posts
    Snippets are awesome. They allow for dynamic content in silly-sweet ways. Often, however, we create Snippets that reuse conditional code that we could have simply typed once. The desire for this often comes from the need to use these same conditions in several different types of Chunks, Template or Resources in different ways. This simple tutorial will show you how to write a Snippet that may be used as either a conditional Chunk or a conditional Function. This saves a lot of keystrokes and duplication of logic. Depending on circumstances, it may also improve processing and memory.

    Here's a simple Snippet that will serve Conditional Chunks: (isLessThan)
    <?php
    // Initialize Properties
    // WE'RE NOT INITIALIZING CHUNKS
        $value = !empty($value)
            ? $value
            : 50;
        $limit = !empty($limit)
            ? $limit
            : $limit;
    
    // Check our Conditions
        if ($value < $limit)
        {//If we have Chunk, process it
             if (!empty($yes))
                 $output = $modx->getChunk($yes, $scriptProperties);
        // Otherwise, return Boolean True
             else
                 $output = true;
        }
    // If we have Chunk, process it
        elseif (!empty($no))
            $output = $modx->getChunk($no, $scriptProperties);
    // Otherwise, use Boolean False
        else 
            $output = false;
    
    // Return the Output no matter what it is.
        return $output;
    


    Simple enough... Now we just have to see it in action. First a Resource.

        [[!isLessThan?
            &value=`25`
            &limit=`30`
            &yes=`chunkOne`  [[- Use any Chunk that you already have]]
            &no=`chunkTwo`   [[- Use any Chunk that you already have]]
            &anyParamForOneOrTwo=`value`   [[- Use as few or as many as you need to pass.]]
            ...
            ...
        ]]
    


    If you run the resource, obviously the 'yes' Chunk will appear. But now we also have this behavior for other Snippets (saving placeholders, memory and sometimes processing).

    Here's another simple Snippet:
    // Call the first Snippet
        $isLessThan = $modx->runSnippet('isLessThan', array('value'=>25, 'limit'=>20));
    // React to the Results
        if ($isLessThan)
            $output = 'We hit the jackpot';
        else
            $output = 'Bummer, dude!';
        return $output;
    


    If you call this Snippet (notice it has no params), you'll get entirely different output than the other call. Now you've just reused a Conditional Snippet as a Function. It's a simple trick,, but often overlooked. This Tip is just a reminder that MODx is cool for much more than content. [ed. note: fuzzicallogic last edited this post 14 years, 1 month ago.]
      Website: Extended Dialog Development Blog: on Extended Dialog
      Add-ons: AJAX Revolution, RO.IDEs Editor & Framework (in works) Utilities: Plugin Compatibility List
      Tutorials: Create Cross-Context Resources, Cross-Context AJAX Login, Template-Based Actions, Remove Extensions from URLs

      Failure is just another word for saying you didn't want to try. "It can't be done" means "I don't know how".
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      You might also want to check out the If snippet, as well as output modifiers. You can make custom output modifiers, too.

      http://rtfm.modx.com/display/ADDON/If
      http://rtfm.modx.com/display/revolution20/Input+and+Output+Filters+%28Output+Modifiers%29
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 39932
        • 483 Posts
        @Susan:

        Great point! Just to provide distinction, Output modifiers apply to calls from within the MODx parser, and therefore apply several possible results of output from the running of a Snippet (or even Chunk).

        This allows reuse of code from within a Snippet AND within the MODx parser. The point of this is to reuse complex logic and bypass the MODX parser when you need a direct result, but still have the code logic be useful when called within the parser.

        The If Addon simply runs conditional parsing, which is different than this tip. This is for code control flow, without losing the ability to have conditional parsing. Just another best of both worlds example for coders. The sample was just a simple demonstration. Awesome highlights though! [ed. note: fuzzicallogic last edited this post 14 years, 1 month ago.]
          Website: Extended Dialog Development Blog: on Extended Dialog
          Add-ons: AJAX Revolution, RO.IDEs Editor & Framework (in works) Utilities: Plugin Compatibility List
          Tutorials: Create Cross-Context Resources, Cross-Context AJAX Login, Template-Based Actions, Remove Extensions from URLs

          Failure is just another word for saying you didn't want to try. "It can't be done" means "I don't know how".