We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3586
    • 27 Posts
    Hi Folks,
    I’m pretty new to modx, but I like it and the way it is developed.
    But because I still miss some functionality and stuff and I’m able to write PHP, I wanted to integrate
    my own functionality in modx.
    So here is my problem:

    Where is the difference between a snippet and a plugin? A module is for the admin-backend, isn’t it? This I do understand,
    but looking at code of snippets and plugins they seem pretty much similar to me .

    I got another problem too:
    I want to integrate an existing gallery (spgm) into my page. I understand the structure of this gallery-tool, but I’m not sure
    where to start at modx. Do I need to write a snippet or a plugin (see my problem above)? And what does a snippet/plugin BASICALLY
    consist of?

    Thanks to all of you for helping. I’m also not sure if this is the right forum for my questions, so, mods feel free to move it!

    Cheers,
    bll0
      • 25663 MODX Staff
      • 12,272 Posts
      A snippet and a plugin are very similar code-wise, but plugins watch system events, and can override how MODx works. A Module can work in the front end or the back end.

      As far as coding advice goes, you definitely don’t want mine, but welcome to MODx. laugh
        Ryan Thrash, MODX Co-Founder
        Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
        • 4095
        • 372 Posts
        As far as I understand and I’m not a coder, is that a plugin will affect all pages. As Ryan mentioned it watches for system events. No doubt you could write a plugin to only affect some pages, but its basic function is to react on any page given the right system trigger, eg OnWebPagePrerender or OnWebLogout.

        A snippet is called from within a page so can be easily applied to single pages, multiple pages, or even multiple times on one page.

        You chose where to put it on the page, by using [[snippet &varibles]] or you can use [!snippet!] to prevent it from being cached. As you can see you can parse the snippet as is, or add variables to alter the configuration when its called, rather than editing the actual snippet code.

        The output of the snippet will be returned at the postion you placed the [[snippet]]

        http://modxcms.com/adding-snippets.html
          [img]http://www.emanz.ac.nz/assets/images/logo/emanz-icon_16x16.gif[/img] Emergency Management Academy of New Zealand [br] http://www.emanz.ac.nz[br][br]MODx Sandbox Login: sandbox Password: castle [br]
          Admin Sandbox Login: sandbox Password: castle
          • 10487 MODX Staff
          • 1,535 Posts
          I see the main differentiation between snippets and plugins is the events that plugins can be hooked onto, as Briggsy says, things like OnWebPagePrerender.

          Generally, snippets will return some form of output whereas a plugin may trigger an action and may not even affect the output of the content. But, the best way to fully understand the differences is to have a play with some of the existing snippets and plugins in the repository - there’s some great examples which you can learn from.

          It’s difficult to pin down a basic structure for a snippet or plugin as they’re so damn versatile, lol! wink

          Quote from: Briggsy at Mar 16, 2006, 02:08 PM
          As far as I understand and I’m not a coder, is that a plugin will affect all pages. As Ryan mentioned it watches for system events. No doubt you could write a plugin to only affect some pages, but its basic function is to react on any page given the right system trigger, eg OnPageRender or OnWebLogout.
          I’d agree, most plugins are designed to run across all pages but, in essence, you could write a module to select the pages that the plugin runs on - an example of this (although not directly linked to a module) is my Autolink plugin that doesn’t run on a page if it is derived from the blank template so as not to risk affecting any RSS feed output. Of course, the next logical step is integrating this selection process into the module.

          As for an image gallery, one way of doing this is to create a module for all the administration (creating galleries, adding images etc.) and then using a snippet to control the output to the page.

          Just my thoughts, Garry
            Garry Nutting
            Senior Developer
            MODX, LLC

            Email: [email protected]
            Twitter: @garryn
            Web: modx.com
            • 3586
            • 27 Posts
            First I’d like to thank all of you guys. I pretty much understand the differences now and will play a little with different snippets and modules.

            Quote from: garryn at Mar 16, 2006, 02:31 PM

            Generally, snippets will return some form of output whereas a plugin may trigger an action and may not even affect the output of the content. But, the best way to fully understand the differences is to have a play with some of the existing snippets and plugins in the repository - there’s some great examples which you can learn from.

            After looking at several smaller snippets, I think the way snippets are build is something like:
            Config some stuff, execute some php code, generate the output in some variable and finally return this output. Right? So a snippet is something like a function with a return value?

            Please correct me if I’m wrong.

            Cheers and good night,
            bll0
              • 10487 MODX Staff
              • 1,535 Posts
              Quote from: bll0 at Mar 16, 2006, 05:43 PM
              After looking at several smaller snippets, I think the way snippets are build is something like:
              Config some stuff, execute some php code, generate the output in some variable and finally return this output. Right? So a snippet is something like a function with a return value?
              In a nutshell, you’ve got it! smiley
                Garry Nutting
                Senior Developer
                MODX, LLC

                Email: [email protected]
                Twitter: @garryn
                Web: modx.com
                • 28042 ☆ A M B ☆
                • 24,524 Posts
                You can also have the snippet dump its output into a MODx placeholder, and it will get displayed wherever you put the [+...+] placeholder tag in your template or document content. You can even have your snippet make several placeholders with different bits of data and scatter them all over your page. Or one snippet call that uses a placeholder and put the same placeholder in several places on your page!

                Most snippets that will generate a form or a specific layout ( such as the WebLogin snippet, or the NewsListing snippet ) also can use a chunk as a template with placeholders in the chunk for the various parts to be displayed, so they are extremely configurable.

                I really appreciated it when the core coders added the abillity to use echo and print statements randomly throughout a snippet; this is great for echoing variables or doing print_r() dumps of arrays such as POST or SESSION to see exactly what’s going on inside the snippet. My favorite:

                echo "<pre>";
                print_r($whatever);
                echo "</pre>";
                


                In all of my bigger multi-file projects, I make a function out of that and put it in my main library file.
                  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