We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26016
    • 561 Posts
    Dear Gurus,
    Here’s a brainteaser for you... I’ve found a situation where I will be using a particular value in 2 places in a template. This value is the result of a snippet call.

    My snippet returns the value I want, and I’ve simply put this snippet call in both places, and it works fine. But then It occurred to me that maybe this is an extra database call, and inefficient.

    Here’s my idea, which may be bunk. Let’s say I set up my snippet so that it returns a value, AND creates a placeholder. This way, the first spot in the template, I make the call and use the Returned value. Then in the second spot (in the template) I use the placeholder value rather than duplicating the first call.

    Depending on how placeholders work, I’m thinking that I could use a stored value for the second template spot rather than doing a second database call. Would I gain anything by doing that, as opposed to using 2 plain Snippet/return calls?

    Thanks!

      MODx and Wordpress development
      Linux, PHP 5.2, MySQL 5.0, Evo 1.05, Revo 2.08-pl, Firefox 4
      • 22303 MODX Staff
      • 10,725 Posts
      Yes, placeholders are perfect for this. Just have the snippet set the placeholder then put the placeholder tags in both spots in your template/chunks/TV’s/content etc. The snippet itself can just return and empty string, and the placeholder can be used as many times as you want. The benefit is reducing the number of snippets being eval()’d per request.
        • 26016
        • 561 Posts
        Quote from: OpenGeek at May 02, 2007, 03:13 PM

        Yes, placeholders are perfect for this. Just have the snippet set the placeholder then put the placeholder tags in both spots in your template/chunks/TV’s/content etc. The snippet itself can just return and empty string, and the placeholder can be used as many times as you want. The benefit is reducing the number of snippets being eval()’d per request.
        Hi,
        Thanks for the info! Excuse my newbie-ness, but if I could trouble you a bit more...

        I had tried before using the placeholder in both spots, but got no output (as I sort of expected). My thought was that I need to make an initial call to populate the placeholder before trying to use the output of the placeholders. That means (I think) making a call to the snippet before putting any other content in the page, which would seem to be inconvenient. Since I’d be using this for title tags, I’d need to make this call in every single page on the site.

        To re-phrase, I’d be making a call to the snippet before any other page content, and then I’d have the placeholder in 2 places in my template. Is that correct? If so, just to make it all easier to code, I might just use my original idea of:

        - making a regular call for the first occurence

        - using the placeholder in the second spot

        Or maybe I’m missing something. Your thoughts?

        Thanks again!!!!

        So I’m
          MODx and Wordpress development
          Linux, PHP 5.2, MySQL 5.0, Evo 1.05, Revo 2.08-pl, Firefox 4
          • 33372
          • 1,611 Posts
          You’re correct that you do have to set the placeholder before using it. You can do that any number of ways, including the one that you describe (calling the snippet in the first occurence and using the placeholder for the second). Or your snippet could just set the placeholder and you could use that in both locations. Or if you prefer you could make your snippet code a plugin instead, have it set the placeholder, and then just use the placeholder wherever you like.
            "Things are not what they appear to be; nor are they otherwise." - Buddha

            "Well, gee, Buddha - that wasn't very helpful..." - ZAP

            Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
            • 26016
            • 561 Posts
            Quote from: ZAP at May 02, 2007, 08:25 PM

            You’re correct that you do have to set the placeholder before using it. You can do that any number of ways, including the one that you describe (calling the snippet in the first occurence and using the placeholder for the second). Or your snippet could just set the placeholder and you could use that in both locations. Or if you prefer you could make your snippet code a plugin instead, have it set the placeholder, and then just use the placeholder wherever you like.
            Zap,
            I’ll read up on that - in my newbie thinking, I don’t see how I can get the snippet to produce my goodies unless I explicitly "tell" it to, (how does it know? smiley) but I see from you folks that it can. No doubt some pre-processing or object-oriented thing that I don’t understand yet. I’ll take a look at the wiki, where it shows what happens when. As an old-school programmer, I’m used to everything being linear and explicitly done, whereas I’m dimly aware that a lot happens automagically in this type of system. grin

            Looking at that stuff helped me when I found that if I coded two snippet calls in a row on a page, some stuff from the second one executed before the first one! And I managed to find a way around it.

            BTW, I’m going to implement your security suggestions (from another thread) sometime this week, combined with some freshly-learned, very restrictive preg_match statements! You rule!

            Thanks again, and I appreciate the help!
            S
              MODx and Wordpress development
              Linux, PHP 5.2, MySQL 5.0, Evo 1.05, Revo 2.08-pl, Firefox 4