We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7231
    • 4,205 Posts
    I am having trouble with a PHx call (within Ditto tpl). What I am trying to do is:
    [+phx:if=`[+tvOne+]`:isnot=``:then=`some html here`:else:if=`[+tvTwo+]`:isnot=``:then=`some other html here`:else=`the last html choice here`+] 


    Basically: if tvOne is not empty load some HTML: else if tvTwo is not empty load some HTML: else load the default HTML.

    I suspect that I am using the ’else:if’ wrong. Can anyone point me in the right direction? grin
      [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

      Something is happening here, but you don't know what it is.
      Do you, Mr. Jones? - [bob dylan]
      • 3749
      • 24,544 Posts
      I’m not a PHX expert at all (full disclosure: I’ve never used PHX wink), but looking at the docs, there’s no "if else" in PHX and the else isn’t really an else, it’s really an else=. T do what you want with just else statements, you’d need the part after the else to be a statement, but it’s not -- it just sets the initial variable equal to something. If you were using a single TV, you could use the PHX select: operator.

      It looks to me like you need a snippet (although it should be a *very* simple one). Someone more familiar with PHX may have a better answer.

      There may be a non-PHX solution using TVs, although I don’t see it.

      I think this would be the snippet you want (more or less):

      <?php
      
      $tv1_out = $modx->getTemplateVarOutput('tv1');
      $tv2_out = $modx->getTemplateVarOutput('tv2');
      
      if (! empty($tv1_out)) {
          $output = 'some html';
      } elseif (! empty(tv2_out)) {
          $output = 'some other html';
      } else{
          $output = 'yet some other html';
      }
      return $output;
      ?>

      You could make this more generic by sending the tv names as parameters in the snippet call like this:

      [!SnippetName? &tv1=`tv1_name`!]
      
      $tv1_out = $modx->getTemplateVarOutput($tv1);






        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
        • 3707
        • 241 Posts
        An "elseif" in PHx would be a great addition but unfortunately it doesn’t exist. You can nest PHx tags though so your call would be:
        [+phx:if=`[+tvOne+]`:ne=``:then=`some html here`:else:`[+phx:if=`[+tvTwo+]`:ne=``:then=`some other html here`:else=`the last html choice here`+]`+]
          • 7231
          • 4,205 Posts
          Thanks Bob and Bob wink

          On one side I am happy I was trying something beyond PHx, but on the other I am frustrated with PHx again. I will stick with snippets since they always work rolleyes

          Again, thanks.
            [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

            Something is happening here, but you don&#39;t know what it is.
            Do you, Mr. Jones? - [bob dylan]
            • 3749
            • 24,544 Posts
            @bob1000, I didn’t know you could nest like that in PHX. Thanks for the info. smiley
              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
              • 16278
              • 928 Posts
              I find PHx’s custom modifier feature much easier than trying to fathom PHx syntax for conditionals, particularly as they are all on one line rather than a clear visual structure.

              If you call a snippet phx:snippetname, you can invoke it from a PHx placeholder with the colon, and feed in parameters after the equals sign. In your snippet, the value of the placeholder is available as the variable $output, and the string following the equals sign is $options. So you could modify BobRay’s snippet code as follows, call the snippet phx:htmlSwitch and invoke it with [+tv1:htmlSwitch=`[+tv2+]`+]. This gets Ditto and PHx to do the dirty work of fetching TV values for you.

              <?php
              $out = "";
              if (! empty($output)) {
                  $out = 'some html';
              } elseif (! empty($options)) {
                  $out = 'some other html';
              } else{
                  $out = 'yet some other html';
              }
              return $out;
              ?>


              KP
                • 7231
                • 4,205 Posts
                That is a good tip. Thanks grin
                  [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

                  Something is happening here, but you don&#39;t know what it is.
                  Do you, Mr. Jones? - [bob dylan]