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

    I am trying to find a way to make a snippet display as plain text on the page and not render on page load. That is I would like to display something like: [!Wayfinder?startId=`0` &firstClass=`first` &hereClass=``!] in a content area. I realise I could just put an empty space in there or an extra character such as [~!Wayfinder?startId=`0` &firstClass=`first` &hereClass=``!] and it would display as plain text and not render but this is not the ideal solution as I want to be able to copy and paste it quickly into the modx site that I am working on (this site is kinda like my library of modx tools)

    There is something already created for modx Revolution that does this (I think its called showSnippet.php), does anyone know of or could help point me in the direction of one that could work in Evolution?

    Thanks.
      • 3749
      • 24,544 Posts
      There are a couple of add-ons for this, but the simplest way is this:

      [<!-- -->[Wayfinder etc.]]
        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
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        You can use whatever you want to break the tags; then use a plugin to remove the character(s) just before the document is sent to the server, using the onWebPagePrerender event.
          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
          • 26016
          • 561 Posts
          I would think that CODE HTML tags would work for that, but that would depend on the MODx parser, I reckon, which may still execute it.
            MODx and Wordpress development
            Linux, PHP 5.2, MySQL 5.0, Evo 1.05, Revo 2.08-pl, Firefox 4
            • 20084
            • 25 Posts
            I would rather be able to put the code snippet in as is, as this resource will be used by various people and would make more sense if they didn’t have to put something into the snippet to *break* it. The code snippet will also be used to display an actual example of the code snippet in action, so I would have one template variable called "snippet" and then in my template I would call it in two places, one place renders it and the other place just shows it as plain text. So ideally I need to "wrap" the second call in something that allows it to pass through the onload event and not render.

            I hope that makes sense.
              • 7455
              • 2,204 Posts
              you can have 2 plugins
              1 that onDocFormSave inserts a break in the snippet call and 1 that removes it onWebPagePrerender like Susan suggested
                follow me on twitter: @dimmy01
                • 3749
                • 24,544 Posts
                There’s a very nice plugin already that does exactly what you want, you just wrap the snippet you don’t want to execute in tags. Unfortunately, I can never remember what it’s called. It’s "pre" something, IIRC.
                  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
                  • 20084
                  • 25 Posts
                  Quote from: Dimmy at Nov 17, 2010, 06:21 AM

                  you can have 2 plugins
                  1 that onDocFormSave inserts a break in the snippet call and 1 that removes it onWebPagePrerender like Susan suggested

                  Hey there, thanks for the responses.
                  I’m not really that up to speed yet with plugins (creating them), how exactly would you go about creating this two plugins?

                  Cheers
                    • 3749
                    • 24,544 Posts
                    OK, I found it -- you only need one plugin. I had to rewrite it a little for MODX 2.x to remove deprecated variables, make it work multiple times on the page, switch the event to one that worked in 2.0, and add <pre></pre> tags to the output. Rahul definitely did the critical work on this one, though, and deserves the credit for it.

                    Tags cut and pasted from your page will work fine, you can use it multiple times on the same page, and other MODx tags on the page outside the <fixedpre><fixedpre> tags will be parsed as usual.

                    Create a plugin called FixedPre.
                    Paste in the code below.
                    On the System Events tag, check the box next to OnParseDocument.
                    Save the plugin.

                    Put the tags you don’t want to execute inside <fixedpre></fixedpre> tags.

                    <?php
                    /** plugin fixedpre  -- implements <fixedpre> tag
                     * MODx code inside <fixedpre></fixedpre> tags will be displayed
                     * rather than executed.
                     * @author Rahul Dhesi
                     * @author Bob Ray
                     * @package fixedpre
                     * Support: // http://rahul.rahul.net/modx-hints/fixedpre-tag.html
                     * 
                     */
                    if (! function_exists('quote_meta') ) {
                        function quote_meta($a) {
                            $lhs = array("<", ">", "[", "]", "!", "{", "}", "`");
                            $rhs = array("<", ">", "[", "]", "!", "{", "}", "`");
                            $b = str_replace("&", "\255", $a[2]);  //save "&"
                    
                            $lhs_preg = array('|<!(!*)fixedpre>|',  '|<!(!*)/fixedpre>|');
                            $rhs_preg = array('<$1fixedpre>',  '<$1/fixedpre>');
                            $b = preg_replace($lhs_preg, $rhs_preg, $b);
                            $b = str_replace($lhs, $rhs, $b);
                            return '<pre>' . str_replace("\255", "&", $b) . '</pre>'; // restore "&" add <pre> tags
                        }
                    }
                    
                    $output =& $modx->resource->_output;
                    $output = preg_replace_callback("#(<fixedpre>)(.*?)(</fixedpre>)#s",
                        "quote_meta", $output);
                    


                    [update] The Forum software inserts #38; before each token. These have to be removed for the plugin to work. The easiest way is to quote this message in a reply and paste the code from there.
                      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
                      • 20084
                      • 25 Posts
                      Hey, Thanks so much for your response,

                      I tried this out and its not working for me yet, I’m sorry but I probably should have stated that I am using Modx 1.0.4, is this going to make much of a difference?

                      Cheers,