We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 630
    • 39 Posts
    I have created the following snippet because I have several docs that handle arbitrary http parameters, and I wanted to simplify accessing them

    <?php
    
    /* Get - Simple access on http parameters
     *
     * Parameters:
     *   &param (mandatory) - name of the GET- or POST-variable
     *   &ph (optional) - instead of simply returning the value, set a placeholder of the name given
     *   &default (optional) - return or set placeholder to this value given, if parameter is not set or empty
     */
    $val = $_REQUEST[$param];
    
    if ( !isset($val) ) {
       if ( !isset($default) ) return;
       else $val = $default;
    }
    
    if ( $ph ) {
       global $modx;
       $modx->setPlaceholder( $ph, $val );
       return;
    }
    else { return $val; }
    ?>
    


    In contradiction to my understanding of the modx caching mechanism so far, this snippet only works when called cached: [[Get? ... - no matter whether or not caching is activated for the document. Thought that [[...]] depends on whether the document caching is active or not, whereas [!...!] is always called uncached independently of the setting.

    Sample code:

    [[Get? &param=`domain` &ph=`domain` &default=`` ]]
    <p>[+phx:if=`[+domain+]`:ifempty=`empty`+]</p>


    sample urls: A /index.php?id=123 and B /index.php?id=123&domain=8

    doc cache active, cached call, A -> "empty"
    doc cache active, cached call, B -> "8"
    doc cache inactive, cached call, A -> "empty",
    doc cache inactive, cached call, B -> "8",
    doc cache active, uncached call, A -> "empty",
    doc cache active, uncached call, B -> "empty",
    doc cache inactive, uncached call, A -> "empty",
    doc cache inactive, uncached call, B -> "empty",

    Could you explain this odd behaviour to me? Are there any subtleties in the implementation of my snippet?

    Thanks for illumination
    in advance. smiley
      • 22303 MODX Staff
      • 10,725 Posts
      The problem here is the phX processing and the order of tag parsing: if you call the snippet with non-cacheable tags, i.e. [!Get!], you are delaying the execution of that snippet until all cacheable content is processed. When you couple that with phX conditionals, the result is that phX evaluates the value of the placeholder before it is set.
        • 630
        • 39 Posts
        Thank you very much, now it’s all clear to me.

        But now I wonder why I can call my snippet cached? Shouldn’t it statically return the same value that it retrieved from the url when the cached document was refreshed?
          • 22303 MODX Staff
          • 10,725 Posts
          Quote from: agilero at Feb 07, 2009, 11:54 AM

          Thank you very much, now it’s all clear to me.

          But now I wonder why I can call my snippet cached? Shouldn’t it statically return the same value that it retrieved from the url when the cached document was refreshed?
          If the page is not cacheable, caching is irrelevant. Otherwise, with a cached snippet and a cacheable page, it will only work the first time because no placeholder value is set the second and it’s a conditional evaluation. Again, I believe this is a side-effect of the 3rd party phX component.