We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 24425
    • 12 Posts
    Seeing some odd behavior - hope this is right place to post. When I use the non-cached call the snippet does not seem to populate the placeholder.

    The ’tester’ snippet code:
    <?php
    global $modx;
    $modx->setPlaceholder("nonform_ph_test", "non-form placeholder text");
    ?>


    Template:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    	<title>Test template</title>
    	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
            <base href="[(site_url)]" />
    </head>
    <body>
    <br />
    
    [[tester]]
    
    Non-Form snippet ph = [+nonform_ph_test+]<br />
    
    [*#content*]<br />
    
    </body>
    </html>


    If I call the snippet as above ([[tester]]), it works fine. If I however change the call to [!test!] it no longer works - so the placeholder no longer seems to be populated.

    Any ideas on what I am doing wrong?

    I’ll have a follow up question on forms, but figured I start here. I’m running 0.9.6.3 if that matters.

    Thanks!
      • 24425
      • 12 Posts
      typo, I do have the non-cached call correct in my code -- "[!tester!]"
        • 24425
        • 12 Posts
        just to be clear the typo was in the post. there was no typo in my code, I still see the issue!
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          When a snippet is non-cached it’s not evaluated at that point; its tags are converted to ordinary cached tags, then the parsing continues. The snippet doesn’t get evaluated until the next pass through the parser, or possibly in the post-processing. I’m still not quite sure at exactly what point placeholders are evaluated, and when unused placeholder are discarded.
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 24425
            • 12 Posts
            Susan, appreciate the response. I guess I can call it the normal way [[snippet]] and make sure the page is not cacheable, but I am confused - I thought one of the main reasons to have placeholders was so that snippets could pass values to other areas of the rendered page that is away from what is returned directly by the snippet... But if I understand you correctly, when you call the snippet uncached, it is processed in a later pass of the parser and the placeholders I want to use have already been evaluated and thrown away?

            I am playing with SESSION variables also. My goal is to have a simple form that the user enters a value and that value is used (displayed, used in a ditto call, etc) in another part (after the snippet call) of the page, but I can’t get it to pass it back through a placeholder. Hence this post...

              • 22303 MODX Staff
              • 10,725 Posts
              The reason this does not work is because you are setting the placeholder only if the snippet is executed, which it will not be when cacheable. However, the only current way to use a placeholder on a cacheable page with a non-cacheable snippet is to use it in the output of that snippet OR in the output of another non-cacheable snippet. IOW, if your placeholder appears within output that is not cached into the partial page cache, it will work.

              That said, and though there is currently no way to indicate (in 0.9.x/1.0) that a placeholder should not be cached, Revolution (2.0) does allow you to use non-cacheable tokens with any tag type. IOW, in Revolution you can specify that a placeholder should not be cached for these kinds of cases, e.g.:
              [[!+nonCacheablePlaceholder]]
              [[!nonCacheableSnippet]]
                • 24425
                • 12 Posts
                Well, that’s the strange part to me. I don’t need to have the page cached, so I unchecked cacheable and tried again.

                When I call the snippet like [[tester]] it works and I see the text, regardless of whether the actual page is cached or not.
                When I call the snippet like [!tester!] I do not see the text.

                I believe sotwell was saying that calling it uncached causes the snippet to be evaluated after the placeholders in the template, which I guess makes sense. But that means that in order to use placeholders I can’t use the uncached call [!snippet!]... Just thought I was missing something.

                I have a similiar issue with setting $_SESSION variables off form input (such as a login) and using a TV with a default value of @EVAL return $_SESSION["Locality"]; to show that session var on every page. No matter where I put the TV, it will return the old value from the page the form snippet is posted to. The next page will show the new value, but that doesn’t help me if I wanted to put a "Welcome username" on the page somewhere.

                thanks again for your responses.

                Bongo
                  • 28042 ☆ A M B ☆
                  • 24,524 Posts
                  You might be better off using a plugin to set a placeholder from the POST and SESSION in that case; using the OnWebPageInit event will ensure the placeholder is set before anything else is done. You would of course use a conditional and only set the placeholder if the POST or SESSION value is set.

                  In other words, if the POST value is set, add it to the SESSION and set the placeholder. If the POST is not set, but the SESSION is set, set the placeholder. If neither are set, do nothing.
                    Studying MODX in the desert - http://sottwell.com
                    Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                    Join the Slack Community - http://modx.org