We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 51481
    • 45 Posts
    Hi folks! I'm trying to use "ifempty" and "notempty" for a banner-style image that's created with a TV (*pageBanner). When no image is added, I need a 52px spacer added for the rest of the page content. Here's what I have so far:

    [[*pageBanner:notempty=`[[$pageImage]]`]]
    [[*pageBanner:ifempty=`<div id="no-image-spacer"></div>`]]


    CSS for #no-image-spacer:

    #no-image-spacer {
    	padding-top: 52px;
    }


    And chunk pageImage:

    <img src="[[*pageBanner]]" alt="" class="img-responsive" style="display: block; max-height: 270px; width: 100%; object-fit: cover; padding-top: 52px;" />


    It's working when there is no image in the TV (creating the spacer div), but when there is an image inserted in the TV, it displays the image and adds the file name below the image (but doesn't create the spacer div). Any idea why it's adding the file name? Seems really bizarre but I'm probably missing something terribly obvious.

    Oh and the website is at http://104.193.173.104/modx/index.php if you want to check out the output of chrome inspector. In the left navigation, Maps has an image and the rest don't.

    Cheers!

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

    [ed. note: matthewmeredith last edited this post 10 years, 9 months ago.]
    • discuss.answer
      • 51481
      • 45 Posts
      Okay, I figured it out myself (with the help of StackOverflow)!

      The chunk pageContent now reads:

      [[*pageBanner:notempty=``]]
      [[*pageBanner:ifempty=`<div id="no-image-spacer"></div>`]]


      ... which calls the pageBanner TV itself (instead of my old system of calling the pageImage chunk to call the TV). Then I changed the output of the TV from "default" to "Image" and added all the properties from my pageImage chunk to their respective fields:

      <img src="[[*pageBanner]]" alt="" class="img-responsive" style="display: block; max-height: 270px; width: 100%; object-fit: cover; padding-top: 52px;" />


      And it works!
        • 24629
        • 370 Posts
        I think you can write that in 1 tag
        [[*pageBanner:notempty=``:else=`<div id="no-image-spacer"></div>`]]

        RDG
          • 3749
          • 24,544 Posts
          Or a much faster method (if I'm understanding what you've done):

          [[!pageBannerCheck]]


          /* pageBannerCheck snippet */
          $pb = $modx->resource->getTVValue('pageBanner');
          return empty($pb)? '<div id="no-image-spacer"></div>' : $pb;


            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