We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37638
    • 63 Posts
    I am having trouble with nested If? snippets.

    Some pages on a site I am working on need to display a login box, others don't. However, if the user is logged in no login box should be displayed:

    [[If?
    &subject=`[[*showLogin]]`
    &operand=`1`
    &then=`<div>
    	[[
    	  [[!If?
    	  &subject=`[[!+modx.user.id]]`
    	  &operator=`eq`
    	  &operand=`0`
    	  &then=`!Login? &tplType=`modChunk` &loginTpl=`loginFormTpl``
    	  ]]
    	]]
      </div>`
    ]]
    


    When I run this (from a different browser, not logged in) I get:
    				  `<div>
    	`
    	  &operator=`eq`
    	  &operand=`0`
    	  &then=`!Login
    				
    


    If I run it with both If?'s uncached, it outputs the login form as expected, but that of course leaves me with a different problem (it will always show what is in the cache, whether or not someone is logged in).

    Am I misunderstanding something about caching and the parsing order here? I'm only NOT caching things that would change outside of cache clears. Or is this a bug?

    MODX Revolution 2.2.5-pl (advanced)

    This question has been answered by BobRay. See the first response.

      • 18373 ☆ A M B ☆
      • 3,141 Posts
      This line:

      &then=`!Login? &tplType=`modChunk` &loginTpl=`loginFormTpl``


      has one too many ` at the end.
        Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

        Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
        • 37638
        • 63 Posts
        One tick is for the &then (for If?) and the second tick is for &loginFormTpl (for !Login?).
          • 18373 ☆ A M B ☆
          • 3,141 Posts
          Ah.

          Well you just found out why nesting conditionals like this is such a problem wink The parser can't know the difference between the two ticks there.

          You can bypass this issue by not specifying the parameters within the nested-nested tag, but instead in a property set that you specify as !Login@FooBar, or in a $chunk instead.
            Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

            Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
            • 37638
            • 63 Posts
            Ok, but if I replace with this code:

            [[If?
            &subject=`[[*showLogin]]`
            &operand=`1`
            &then=`<div>
              [[!If?
              &subject=`[[!+modx.user.id]]`
              &operator=`eq`
              &operand=`0`
              &then=`[[!Login? &tplType=`modChunk` &loginTpl=`loginFormTpl`]]`
              ]]
              </div>`
            ]]
            


            I still get:

            				  `<div>
              `
              &operator=`eq`
              &operand=`0`
              &then=`[[!Login
            


            Does this mean I can't nest If? snippets (or anything else that requires parameters) at all? (Say, I have to use another chunk for every nested If? level?)
              • 3109 ☆ A M B ☆
              • 894 Posts
              Have you tried something like this?

              LoginChunk:

              !Login? &tplType=`modChunk` &loginTpl=`loginFormTpl`
              


              [[*showLogin:eq=`1`:then=`<div>
              
                    [[!If?
                    &subject=`[[!+modx.user.id]]`
                    &operator=`eq`
                    &operand=`0`
                    &then=`[[$loginChunk]]`
                    ]]
              
                </div>`]]
              


              Not sure if that will work or how it will affect performance but it's worth a try.

              Good Luck.
                Benjamin Marte
                Interactive Media Developer
                Follow Me on Twitter | Visit my site | Learn MODX
              • discuss.answer
                • 3749
                • 24,544 Posts
                Wouldn't this be simpler and faster, or am I missing something?

                [[!UserLogin]]


                /* UserLogin snippet */
                return $modx->user->hasSessionContext($modx->context->get('key'))
                    ? $modx->getChunk('loginTpl')
                    : '';
                


                or, if you don't want to hard-code the chunk name:

                return $modx->user->hasSessionContext($modx->context->get('key'))
                ? $modx->getChunk($scriptProperties['loginFormtpl'])
                : '';

                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting
                  • 37638
                  • 63 Posts
                  Bob,

                  That is definitely the most straight forward solution, don't know why it didn't come to mind.

                  I still am curious, however: is it ever possible to nest snippets/chunks with set parameters? I.e., will the backtick problems always occur? Or, will they only occur when the outer snippet is processed first? (https://www.markhamstra.com/modx-blog/2011/11/nested-caching-in-modx-revolution/)
                    • 18373 ☆ A M B ☆
                    • 3,141 Posts
                    It is possible, but when you get ticks in the mix it can become too complicated for the parser. That's when you use the conditionals to build a chunk tag; the chunk can then contain any ticks you want as it wont interfere with the outer conditional.
                      Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

                      Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
                      • 37638
                      • 63 Posts
                      Thanks Mark, Bob, Ben!