We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 33996
    • 20 Posts
    I've got a resource calling a snippet with a parameter that is a call to the "getUrlParam" snippet extra.

    My problem is when the resource calls the snippet in cached mode, the value of the getUrlParam has a strange behaviour and is wrong...

    Here the code that produces the problem I meet:

    First call of nullSnippet cached:
    [[nullSnippet?test=`[[!getUrlParam? &name=`test`]]`]]
    
    Second call uncached:
    [[!nullSnippet?test=`[[!getUrlParam? &name=`test`]]`]]
    


    The nullSnippet does this:
    <?php
    
    echo "nullsnippet: $test<br>"; //ok show the correct value
    
    $aString = 'aString:'.$test;
    echo "nullsnippet: $aString <br>";  //ok show the correct value
    
    $test= (int) $test; //cast the value in a integer...
    
    echo "nullsnippet: $test<br>";  //KO if the snippet is cached/ OK if uncached!?
    
    



    When I call my resource page as testbug.html?test=10
    I obtain

    First call of nullSnippet cached:
    nullsnippet: 10
    nullsnippet: aString:10
    nullsnippet: 0  !!!! What's this!?
    
    Second call uncached:
    nullsnippet: 10
    nullsnippet: aString:10
    nullsnippet: 10
    


    Calling the snippet in uncached mode gives a correct return.
    What's wrong here when the snippet is called cached?

    This question has been answered by multiple community members. See the first response.

      • 30585
      • 833 Posts
      I'm not sure I understand your question, but you are missing an ampersand in your snippet call.

      Change this:
      [[nullSnippet?test=`[[!getUrlParam? &name=`test`]]`]]

      to this:
      [[nullSnippet? &test=`[[!getUrlParam? &name=`test`]]`]]
        A MODx Fanatic
      • discuss.answer
        • 4172
        • 5,888 Posts
        this doesn't make much sense:

        [[nullSnippet?test=`[[!getUrlParam? &name=`test`]]`]]
        


        you are trying to pass the result of an uncached snippet to a cached snippet.
        This can't work.

        The parser parses cached tags first. In a next step it parses uncached tags.
        So, at the time, when your nullSnippet is executed, getUrlParam wasn't executed.
        what nullSnippet does in this case with

        echo "nullsnippet: $test<br>"; //ok show the correct value


        is just output this:

        nullsnippet: [[!getUrlParam? &name=`test`]]<br>


        getUrlParam is parsed in a next step and return the correct value of 10

        with

        $test= (int) $test; //cast the value in a integer...


        your snippet tries to convert the unparsed tag of

        [[!getUrlParam? &name=`test`]]
        to int, which results in 0, of course.


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

          you can buy me a beer, if you like MIGX

          http://webcmsolutions.de/migx.html

          Thanks!
          • 33996
          • 20 Posts
          @Bruno17, Your explanation is crystal clear!
          Thanks for it...

          So... is it possible to produce a cache version of the snippet in order that if the same GET parameter is sent twice, the snippet could give the cache version the second time? like to force the parsing of the uncached snippet first then the cached one?
          Or should I need to program something with the modx cache mecanism (https://docs.modx.com/revolution/2.x/developing-in-modx/advanced-development/caching)?

          @treigh, Thanks to have notice this syntax error... I'm doing it regularly! unfortunatly, in that case, it did not solve the problem...
          • discuss.answer
            • 4172
            • 5,888 Posts
            getCache should do what you want
            https://modx.com/extras/package/getcache
              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!
              • 33996
              • 20 Posts
              sounds perfect! thanks