We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 42088
    • 7 Posts
    I'm new to MODX and not great at php. Having fun with is so far smiley

    I think I need the equivalent of an if elseif else statement so I can display menuA in case1, menuB in case2, menuC in case3 and menuD in all non-defined cases.

    I've researched a good bit and can't seem to find a decisive path to take.
    I recently came across Switch.
    http://modx.com/extras/package/switch

    Has anyone used it?

    Here's what I have so far. All cases work but the default doesn't output the menu. The default will output text, but not the content of a chunk using getResources or wayfinder.

    [[!switch?
    &get=`[[!UltimateParent]]`
    &c1=`2` &do1=`[[$menuA]]`
    &c2=`3` &do2=`[[$menuB]]`
    &c3=`6` &do3=`[[$menuC]]`
    &default=`[[$menuD]]`
    ]]

    I'd be interested in a solution that doesn't involve Switch, if anyone has a better approach for what it is I am trying to achieve. Or a simple correction, if that's what's needed.

    Thanks [ed. note: left23 last edited this post 13 years, 9 months ago.]
      • 10208 ☆ A M B ☆
      • 1,780 Posts
      I couldn't get switch to output any default last week either although it works in the template without chunks for different TV's. I'm gonna try again later.

      What happens if you don't use chunks, but rather the chunk code itself?

      It's a little unclear what &get=`2` is, but I think you may need to issue the call here instead of using 2.
       &get=`[[getResources? &resources =`11` &tpl=`yourTpl` &includeTVs=`1` &processTVs=`1` &tvPrefix=``]]
      `
      OR
      [[Wayfinder? &startId=`[[*id]]`...]]


      You can use the If snippet, or just plain conditionals. below [[*2]] would be your TV name, not 2. It also assumes *2 can be empty.

        [[If? &subject=`[[*2]]` &operator=`EQ` &operand=`2` &then=`[[$menuA]]`]]
        [[If? &subject=`[[*2]]` &operator=`EQ` &operand=`3` &then=`[[$menuB]]`]]
        [[If? &subject=`[[*2]]` &operator=`EQ` &operand=4` &then=`[[$menuC]]`]]
        [[If? &subject=`[[*2]]` &operator=`` &then=`[[$menuD]]`]]


      Conditionals:
       [[*2:is=`2`:then=`[[$menuA]]`:else=``]] 
       [[*2:is=`3`:then=`[[$menuB]]`:else=``]] 
       [[*2:is=`4`:then=`[[$menuC]]`:else=``]] 
      [[*2:is=``:then=`[[$menuD]]`:else=``]]


      I might also try it with the default set to menuD, and omit the last conditional above. Not sure which is best, would have to test it.
      [[*2:is=`2`:then=`[[$menuA]]`:else=`[[$menuD]]`]]


      Now, with all that said, there's a lot of parsing going on there. For such conditionals, read this first. http://modx.com/blog/2012/09/14/tags-as-the-result-or-how-conditionals-are-like-mosquitoes/

      It states that the above conditionals should actually be written like this so that MODX doesn't have to parse everything just to get to the conditional bit (haven't gotten it working yet myself, play with it?):
      [[[[*2:is=`3`:then=`$menuB`:else=`&menuD`]]]]
        Frogabog- MODX Websites in Portland Oregon
        "Do yourself a favor and get a copy of "MODX - The Official Guide" by Bob Ray. Read it.
        Having server issues? These guys have MODX Hosting perfected - SkyToaster
        • 42088
        • 7 Posts
        Quote from: frogabog at Jan 08, 2013, 01:15 PM

        It's a little unclear what &get=`2` is, but I think you may need to issue the call here instead of using 2.

        Hi Frogabog,

        Thanks for the quick reply. I'll process it more fully in a while (break time for me at the mo smiley)

        correct &get=`2` to &get=`[[!UltimateParent]]`

        2 was just my leftover testing.

        I edited my post a few minutes after I posted. Sorry bout that.

        [[!switch?
        &get=`[[!UltimateParent]]`
        &c1=`2` &do1=`[[$menuA]]`
        &c2=`3` &do2=`[[$menuB]]`
        &c3=`6` &do3=`[[$menuC]]`
        &default=`[[$menuD]]`
        ]]
          • 10208 ☆ A M B ☆
          • 1,780 Posts
          Ahh, I see you've modified &get to include UltimateParent.

          You need to call Wayfinder first, and use &startId=`[[!UltimateParent]]` within your Wayfinder call.

          EDIT: UltimateParent is not a stand alone snippet. It works within Wayfinder, in the &startId parameter.
            Frogabog- MODX Websites in Portland Oregon
            "Do yourself a favor and get a copy of "MODX - The Official Guide" by Bob Ray. Read it.
            Having server issues? These guys have MODX Hosting perfected - SkyToaster
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            Actually UltimateParent is just a snippet like any other snippet; it simply returns the ID# of the top-level parent of the resource. It can be used in anything that needs to have the ID of the grandparent or great-grandparent or however high up the tree you need to go.
              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
              • 10208 ☆ A M B ☆
              • 1,780 Posts
              Thank you for correcting me Susan. I didn't know that.
                Frogabog- MODX Websites in Portland Oregon
                "Do yourself a favor and get a copy of "MODX - The Official Guide" by Bob Ray. Read it.
                Having server issues? These guys have MODX Hosting perfected - SkyToaster
                • 28042 ☆ A M B ☆
                • 24,524 Posts
                * @param &id The id of the document whose parent you want to find.
                * @param &top The top node for the search.
                * @param &topLevel The top level node for the search (root = level 1)
                *
                * @license Public Domain, use as you like.
                *
                * @example [[UltimateParent? &id=`45` &top=`6`]]
                * Will find the ultimate parent of document 45 if it is a child of document 6;
                * otherwise it will return 45.
                *
                * @example [[UltimateParent? &topLevel=`2`]]
                * Will find the ultimate parent of the current document at a depth of 2 levels
                * in the document hierarchy, with the root level being level 1.
                  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