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

    I'm trying to add some randomness to the result of getResources by calling a snippet inside the chunk that getResources use. The snippet should return a random item from a list of css-classes.

    This does not work as I'd expected. It seems that the snippet gets called only once and this result ends up being used for all resources.

    GetResources-call:
    [[getResources? &parents=`[[*id]]` &sortby = `{"longtitle":"ASC"}` &tpl=`productIconRowTpl` &limit=`0` &includeTVs=`1` &processTVs=`1`]]

    Snippet "rotate":
    <?php
    $array = array("rotate-7", "rotate-5", "rotate-3", "rotate3", "rotate5", "rotate7");
    shuffle($array);
    return $array[0];
    ?>


    Chunk "productIconRowTpl":
    <li><a href="[[~[[+id]]]]"><img src="[[+tv.icon:phpthumbof=`w=200&h=280`]]" class="left fancy [[!rotate]]"></img></a></li>


    What am I doing wrong here?

    / Henrik
      • 32699 ☆ A M B ☆
      • 427 Posts
      The snippet call is cached, so it only keeps the first value it grabs. Try calling it uncached; [[!rotate...
        Get your copy of MODX Revolution Building the Web Your Way http://www.sanitypress.com/books/modx-revolution-building-the-web-your-way.html

        Check out my MODX || xPDO resources here: http://www.shawnwilkerson.com
        • 43237
        • 7 Posts
        Thanks, but I tried calling uncached. Both getResources and the snippet. There seems to be some problem with the snippet itself, I can't figure out why. If I just add a few snippet calls to the template:

        [[!rotate]] [[!rotate]] [[!rotate]] [[!rotate]]

        I get:

        rotate3 rotate3 rotate3 rotate3

        or on another pagerequest:

        rotate-7 rotate-7 rotate-7 rotate-7

        It does it's thing once per pagerequest but caches? Why? I use the "!" on each call.

        - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

        Aaaaand I found a workaround!

        If I add a property to the call (in the chunk or in the template), it works!

        In the chunk:
        [[!rotate? &genesimmons=`[[+id]]`]]

        The property needs to be something non static passed from getResources though.

        But in a template I have to do something like this to get different results from each call:
        [[!rotate? &genesimmons=`[[*id]]`]] [[!rotate? &paulstanley=`[[*id]]`]] [[!rotate? &petercriss=`[[*id]]`]]


        Why is there some additional caching going on even if I use the "!" method?
          • 16899
          • 284 Posts
          I´m not 100% sure if i am understanding you, but if you want to randomize your getresources results use
          &sortby=`RAND()`
          in your plugin call!!!
            • 43237
            • 7 Posts
            Well thanks but my question was about snippets and why they don't get called for each item in my getResources result rendering. Sorry for my convoluted explination. It seems that calling a snippet uncached is not enough, I have to trick it to not cache the result. Maybe it is a setting that's wrong.
              • 4172
              • 5,888 Posts
              snippet-results with its tag-footprint are stored for the same request.
              Also when they are called uncached.

              As soon as you want to have different snippet-results on the same request, you need to have a different tag-footprint for each.
                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 43237
                • 7 Posts
                Ok, I can sort of understand why MODX does this, even if I think it's dumb. So I need to do as I described earlier with unique bullcrap property values.

                My take on this is; either it's cached or it's not. I don't want to have to tell MODX to really really NOT cache this. Maybe a tag like [[!!Snippet]] could mean: "Don not ever cache this". Maybe the snippet output will differ even if no propery is changed, I can think of lots of scenarios where this can happen.

                Is there a way to tell MODX to "foggetaboutit" programmatically, at the end of my snippet by manipulating the cache handler? Probably easier to just change the tag-footprint I guess.
                  • 32699 ☆ A M B ☆
                  • 427 Posts
                  Quote from: hekama at Jun 07, 2013, 05:59 AM
                  Thanks, but I tried calling uncached. Both getResources and the snippet. There seems to be some problem with the snippet itself, I can't figure out why. If I just add a few snippet calls to the template:

                  [[!rotate]] [[!rotate]] [[!rotate]] [[!rotate]]

                  I get:

                  rotate3 rotate3 rotate3 rotate3

                  or on another pagerequest:

                  rotate-7 rotate-7 rotate-7 rotate-7

                  I would suggest you implement a better randomization application.

                  Try something like

                  <?php
                  $values = array(3,7);
                  return array_rand($values);
                  



                    Get your copy of MODX Revolution Building the Web Your Way http://www.sanitypress.com/books/modx-revolution-building-the-web-your-way.html

                    Check out my MODX || xPDO resources here: http://www.shawnwilkerson.com