We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10746
    • 126 Posts
    How do YOU create snippets?

    What I mean is - how do you actually develop and debug a snippet? At the moment I am editing in the manager and then browsing to the snippet-containing page and trying to chase up errors via the server’s error logs....

    Is there a better way of developing and debugging "offline" and then just adding the perfected snippet?

    I’m using the DBAPI (though accessing a separate database) so I guess there is no alternative?

    Does "execute snippet after saving" play a role here? I sometimes check that for fun, but nothing ever seems to happen...

    Thx

    Gordon

    PS One more question: how do you make a snippet assign values to placeholders?
      • 22303 MODX Staff
      • 10,725 Posts
      Quote from: gfroyle at Dec 11, 2006, 08:52 PM

      How do YOU create snippets?

      What I mean is - how do you actually develop and debug a snippet? At the moment I am editing in the manager and then browsing to the snippet-containing page and trying to chase up errors via the server’s error logs....

      Is there a better way of developing and debugging "offline" and then just adding the perfected snippet?
      I always create my snippets as PHP files and simply include them in the snippet code (1 line snippets). This way, I get a line number and I can use my IDE to edit it easily and reload without messing with the manager. This has other, less obvious benefits too, like reducing your MODx cache file sizes, and allowing byte-code cache systems to cache some of your snippet logic.

      Quote from: gfroyle at Dec 11, 2006, 08:52 PM

      I’m using the DBAPI (though accessing a separate database) so I guess there is no alternative?
      xPDO will be the alternative in the very near future, but you can basically connect to a database in any way your PHP environment will allow you too. DBAPI is just a convenient way to do so, and xPDO will bring even more benefits for this kind of data interaction from within the MODx API...

      Quote from: gfroyle at Dec 11, 2006, 08:52 PM

      Does "execute snippet after saving" play a role here? I sometimes check that for fun, but nothing ever seems to happen...
      I ignore it; have no idea what practical purpose it would serve really...

      Quote from: gfroyle at Dec 11, 2006, 08:52 PM

      PS One more question: how do you make a snippet assign values to placeholders?
      $modx->setPlaceholder('placeholder_name', $someValue);
        • 10746
        • 126 Posts
        Thanks for that reply.. very helpful.

        Except for one thing - I now realize that I do not understand how placeholders work in general, in that I can’t work out who/what is responsible for them!

        What I am trying to do is understand the sentence (from the MODx tags documentation) that says "A placeholder can be used anywhere in any HTML code where you wish that particular piece of a snippet’s output to appear."

        Does this mean that

        - a snippet simply has to SET values for the placeholders (using setPlaceholder) and then something else actually does the "replacement"

        or

        - a snippet must actually PROCESS the placeholder-containing HTML (maybe passed to it via a chunk parameter) and is ITSELF responsible for doing the actual replacement

        (or some mixture of both).


        Cheers

        Gordon



          • 15987
          • 786 Posts
          Gordon,
          All you need to do is set the value of a placeholder in your snippet. Then as the page passes through the parser the placeholders are replaced by the value set in the snippet.

            • 10746
            • 126 Posts
            Quote from: kylej at Dec 13, 2006, 07:25 AM

            Gordon,
            All you need to do is set the value of a placeholder in your snippet. Then as the page passes through the parser the placeholders are replaced by the value set in the snippet.

            But that’s not what Wayfinder does is it?

            I was looking at the Wayfinder PHP (wayfinder.inc.php) to try to figure out more about how things work.. as far as I could see, Wayfinder grabs a chunk (either dfault or user specified) and then actually replaces ITSELF the placeholders with values for each list-item that it creates...

            So I can’t just use [+wf.linktext+] in some arbitrary piece of HTML and have anything sensible happen.. in fact, I wouldn’t expect to be able to do so, because obviously wf.linktext changes for each list-item, and as far as I can see Wayfinder NEVER actually does "setPlaceholder" for that placeholder..


            I DO realize that I can use &ph to cause Wayfinder to assign the entire output to a user-nominated placeholder and then use it somewhere else in the page, so presumably this uses the mechanism you refer to, but this seems to be the exceptional case, not the normal case.

            Cheers

            Gordon

            PS You have a very very minor typo on the muddydogpaws Wayfinder documentation where textOfLinks refers to "wflinktext" without the dot.



              • 15987
              • 786 Posts
              Gordon,
              You are correct, it is not how I do it in wayfinder. The part of Wayfinder you are referring to is the templating and since all of those parts must be nested together and most of them are used multiple times you have to have the snippet replace the values.

              I would say the typical use of the setPlaceholder function, would be for when you want to output multiple items from a snippet that may not necessarily need to be in an order. It can also be used like in wayfinder to place the output anywhere not just where the snippet is called. for example you could have a snippet that creates some dynamic page header and footer, if you set each one to a seperate placeholder, then all you have to do is call the snippet once and put both of the plceholders in the template.

              So really it depends on what you are trying to accomplish, if you use setPlaceholder that value will get inserted anywhere you have the placeholder, but if you are templating it will probably be handled by the snippet.

              Hope that helps smiley

              Kyle


              p.s. thanks for pointing out the typo, I will get that fixed.
                • 10746
                • 126 Posts
                Quote from: kylej at Dec 13, 2006, 06:38 PM

                I would say the typical use of the setPlaceholder function, would be for when you want to output multiple items from a snippet that may not necessarily need to be in an order. It can also be used like in wayfinder to place the output anywhere not just where the snippet is called.

                Yes, it certainly helps..

                But what I conclude from my investigation and this thread is that there are TWO quite distinct major ways of using placeholders (and probably other minor ways)...

                Placeholders can either be used as part of the INPUT to a snippet where they will be replaced with values dynamically generated by the snippet which will produce output with all placeholders gone... we can maybe call this a "placeholder-consuming snippet"

                Or placeholders can be the fundamental OUTPUT of the snippet in that a snippet can assign values to multiple placeholders that can be used by the page designer in any part of the document in any particular order... we can call this a "placeholder-producing snippet"


                My original, now-cleared-up, confusion was because I had no concept of the "consumer/producer" dichotomy..

                The documentation that I was trying to understand invokes both concepts, but without separating them.. the NewsListing example is a "placeholder-consumer" while the Personalize example is a "placeholder-producer".

                Cheers

                Gordon