We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38590
    • 22 Posts
    Is it possible to add an IF statement in MODX (revo) templates, and how?
    Or is there a better solution for this kind of problem?

    I just don't want my containing HTML to appear if the certain field is not filled in.

    Like:
    IF page has description{
       <certain>
          <html>[[*description]]</html>
       </certain>
    }


    I came acros this: http://rtfm.modx.com/display/revolution20/Input+and+Output+Filters+%28Output+Modifiers%29

    but it seems to me that's for modifying output OF a parameter, not to output certain code IF a certain item exists.

    Thx in advance, grts

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

      • 3749
      • 24,544 Posts

      You don't need the if. I think this will do it:

      <html>[[*description:isempty:then=``:else=`[[*description]]`]]</html>


      Depending on how you're using it, you might rather use the pagetitle or something else if it's empty.


      ---------------------------------------------------------------------------------------------------------------
      PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
      MODx info for everyone: http://bobsguides.com/modx.html
        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
        • 38590
        • 22 Posts
        Thx for the reply

        My description will need certain markup AROUND it for styling purposes, and I don't want that HTML to appear in the document if the description is not filled in.

        Is this possible:

        [[*description:isempty:then=``:else=`
           <div class="description">
              [[*description]]
           </div>
        `]]
        


        And is it the best solution?

        Thx!
          • 3749
          • 24,544 Posts
          Well, the best solution is almost always a custom snippet, but that should work.

          The snippet would look something like this:

          [[!MyDescription]]


          <?php
          /* MyDescription snippet */
          
          $description = $modx->resource->get('description');
          
          return empty($description) ? "" : 
          '<div class="description">'
              .  $description . 
          '</div>';



          Or slightly less efficient, but separating code and content better (with the html in a Tpl chunk):

          /* MyDescription snippet */
          
          $description = $modx->resource->get('description');
          
          return empty($description) ? "" : 
             $modx->getChunk('DesChunk', array('md,description' => $description));
          



          DesChunk Tpl Chunk:


          <div class="description">
                [[+md.description]]
          </div>
          








          ---------------------------------------------------------------------------------------------------------------
          PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
          MODx info for everyone: http://bobsguides.com/modx.html
            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
          • discuss.answer
            • 19369
            • 1,098 Posts
            Hi,

            I think you are looking for the If Snippet or the Andif Snippet.

            Here is an example using your code:
            [[If? 
            	&subject=`[[*description]]` 
            	&operator=`notempty` 
            	&then=`
            
               <div class="description">
                  [[*description]]
               </div>
            
            `]]
              • 36416
              • 589 Posts
              This is one of the first PHx modifiers that I wrote back in Evolution, and it could be used as "output filter" snippet in Revo:

              <?php
              $output = trim($input);
              if (!empty($output)) {
                if ($pos = strpos($options, " ")) {
                  $closetag = substr($options, 0, $pos);
                } else {
                  $closetag = $options;
                }
                $output = "<{$options}>{$output}</{$closetag}>";
              }
              return $output;


              Usage:
              [[*description:tagwrap=`div class="description" id="idescr"`]]
                • 38590
                • 22 Posts
                BobRay:
                I prefer the "less efficient" but more separated approach, but it would be a hassle creating a custom snippet and chunk everytime I want to include some part of a document with wrapper html-tags.


                Microcipcip:
                That's the closest to what I was looking for, and closest to what I'm used to in ExpressionEngine.
                EE has this:
                {if username == "joe"}
                        <h1>Hey, Joe! Where were you Tuesday?</h1>
                {if:elseif username == "bob"}
                        <h1>Hey, Bob! Thanks for the tickets!</h1>
                {if:else}
                        <h1>Welcome to our site.</h1>
                {/if}
                


                I think that's pretty handy AND clean, I think it would be a good idea if MODx had something like that by default. Am I the only one?


                Eoler:
                Problem with that solution is: it's limited to one wrapper tag, or am I mistaken?


                I'll be checking the If and AndIf snippets.
                Thx guys!

                Grtz
                  • 36416
                  • 589 Posts
                  Quote from: kennybrijs at Feb 07, 2012, 09:28 AM
                  (...)
                  I think that's pretty handy AND clean, I think it would be a good idea if MODx had something like that by default. Am I the only one?
                  Eoler:
                  Problem with that solution is: it's limited to one wrapper tag, or am I mistaken?

                  Yes, a simple single-tag wrapper.

                  BTW, I don't like Smarty-like syntax above - display logic needs to go to "views" (snippets), language topics to lexicons etc.
                    • 3749
                    • 24,544 Posts
                    Quote from: kennybrijs at Feb 07, 2012, 09:28 AM
                    BobRay:
                    I prefer the "less efficient" but more separated approach, but it would be a hassle creating a custom snippet and chunk everytime I want to include some part of a document with wrapper html-tags.


                    Microcipcip:
                    That's the closest to what I was looking for, and closest to what I'm used to in ExpressionEngine.
                    EE has this:
                    {if username == "joe"}
                            <h1>Hey, Joe! Where were you Tuesday?</h1>
                    {if:elseif username == "bob"}
                            <h1>Hey, Bob! Thanks for the tickets!</h1>
                    {if:else}
                            <h1>Welcome to our site.</h1>
                    {/if}
                    

                    I think that's pretty handy AND clean, I think it would be a good idea if MODx had something like that by default. Am I the only one?
                    Grtz

                    I think this is the equivalent (using the switch snippet):

                    [[!switch?
                        &get=`[[+modx.user.username]]`
                        &c1=`joe` &do1=`<h1>Hey, Joe! Where were you Tuesday?</h1>one`
                        &c2=`bob` &do2=`<h1>Hey, Bob! Thanks for the tickets!</h1>`
                        &default=`<h1>Welcome to our site.</h1>`
                      ]]  
                    



                    ---------------------------------------------------------------------------------------------------------------
                    PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
                    MODx info for everyone: http://bobsguides.com/modx.html [ed. note: BobRay last edited this post 14 years, 7 months ago.]
                      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
                      • 38590
                      • 22 Posts
                      Well, I got enough information to solve the problem anyways, but if you'll allow me to nitpick for a moment: I like the If and Switch snippets (and thanks to the people who made it!), it's just that they have a limitation (as I said, it's not a problem in this case, but it doesn't allow me to use them in EVERY situation)

                      "If" doesn't have an elseif, and Switch only allows EXACT comparisons (e.g.: $x = 3) but not more complex comparisons (e.g.: $x < 3)

                      Well, anyways, it's probably possible to call a snippet of my own in the templates and work some PHP magic smiley But I think a more versatile (and integrated by default) solution would be a nice feature for MODx.