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

    (I’m new to modx, so please be kind if this is a stupid question)

    I’d like to insert my snippet several times on a page. The snippet shall include a variable that works as a counter and should be incremented everytime the snippet is called. I used "$modx->setPlaceholder(’Zaehle’, 1);" and "$modx->getPlaceholder(’Zaehle’);" to create that counter.

    This doesn’t work right. When I check the variable from different snippets, it’s OK, but when I use the same snippet on a single page, it appears cached. The counter is indeed incremented (in the background) but the output on the page doesn’t change.

    Its a bit like that:

    Call snippet A (counter is set to 1)
    -> counter: 1

    Call snippet B (counter is incremented by 1)
    -> counter: 2

    Call snippet B again (counter is incremented by 1)
    -> counter: 2

    Call snippet B again (counter is incremented by 1)
    -> counter: 2

    Call snippet C (just show actual value of counter)
    -> counter: 4

    What can I do about it? I always inserted the snippets by [!Name!] and I unchecked the cachable-checkbox on the page.

    Thanks for any help!
    Donalbain
    • Not sure there’s much we can do without seeing the snippet code. Feel free to post it in some code tags and someone will surely help.

      Although if you are putting the placeholder in the content that is cacheable, then the answer might be more obvious that you think...the entire page would need to be cacheable in that case.
        • 7806
        • 45 Posts
        OK, here’s a minimal example:

        This is the page (with setting "Cacheable: No"):
        <h3>Demo</h3>
        [!Demo-Start!]
        <br />
        [!Demo-Middle!]
        <br />
        [!Demo-Middle!]
        <br />
        [!Demo-Middle!]
        <br />
        [!Demo-Middle!]
        <br />
        [!Demo-Middle!]
        <br />
        [!Demo-End!]
        


        Where the snippets are defined as:
        Demo-Start
        $modx->setPlaceholder('Zaehle', 0);


        Demo-Middle
        $A=$modx->getPlaceholder('Zaehle');
        $A++;
        echo $A;
        $modx->setPlaceholder('Zaehle', $A);


        Demo-End
        $A=$modx->getPlaceholder('Zaehle');
        echo $A;
        


        I would expect to get
        1
        2
        3
        4
        5
        5

        But instead I get
        1
        1
        1
        1
        1
        5

        So what is wrong here? How can I get the desired result?
        Thanks for any hints!

        Donalbain
        • Try this:

          <?php
          $A=& $modx->getPlaceholder('Zaehle');
          $A++;
          echo $A;
          // no need for the following code anymore, because
          // you operate on a reference to the placeholder itself
          // rather than a copy of it
          //$modx->setPlaceholder('Zaehle', $A);
          ?>


          or

          <?php
          $A=$modx->getPlaceholder('Zaehle');
          $A++;
          $modx->setPlaceholder('Zaehle', $A);
          echo $modx->getPlaceholder('Zaehle');
          ?>
            • 7806
            • 45 Posts
            Hmm, doesn’t help. I still get 1 1 1 1 1 5.

            What does help (but it’s just an ugly workaround) is to insert the snippets with different useless and senseless arguments:

            [!Demo-Middle&w=1!]
            [!Demo-Middle&x=1!]
            [!Demo-Middle&y=1!]
            [!Demo-Middle&z=1!]

            after that all is well. So it seems in the case without arguments that there’s still some caching going on when the snippet is called by the same name over and over again. Could it be a bug in modx?

            Donalbain