We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 47247
    • 4 Posts
    creatingvibrance Reply #1, 10 years ago
    Hi,

    I'm new to Modx and I want to test if a user use a template variable for image then display that image. If not, display the featured image. I have this code:

    [[ +tv.packery_item_img:ifempty :then=`<img src="[[+tv.featured_image]]">`:else=`<img src="[[+tv.packery_item_img]]">`]]


    That displays the "then" part but not the else. I'm sure that featured_image has value because when I do this:

    <img src="[[+tv.featured_image]]">


    It outputs a value. But when I put it on my first code with the condition, it <img> doesn't have a source. the "then" works and it only inputs <img> tag without the "src"

    Thanks
    van

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

    • Try it this way:
      [[+tv.packery_item_img:ifempty=`<img src="[[+tv.featured_image]]">`:else=`<img src="[[+tv.packery_item_img]]">`]]

        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
        • 47247
        • 4 Posts
        creatingvibrance Reply #3, 10 years ago
        Thanks for the quick reply sottwell,

        It still doesn't output what I need. I just realized that it seems that the "else" is the only one that is working.

        I have two pages. Those two pages has an uploaded TV Featured Image but only one has TV Packery image. when I test it (with your code)

        [[+tv.packery_item_img:ifempty=`empty`:else=`not empty`]]


        Both output is not empty. So it seems like tv.packery_item has a value even if it doesn't have any uploaded image to it.

        What can be the problem?

        thanks.
        • discuss.answer
          Try it like this:
          [[+tv.packery_item_img:is=``:then=`<img src="[[+tv.featured_image]]">`:else=`<img src="[[+tv.packery_item_img]]">`]]

            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
            • 47247
            • 4 Posts
            creatingvibrance Reply #5, 10 years ago
            Quote from: sottwell at Apr 16, 2014, 07:54 AM
            Try it like this:
            [[+tv.packery_item_img:is=``:then=`<img src="[[+tv.featured_image]]">`:else=`<img src="[[+tv.packery_item_img]]">`]]


            That works like a charm! Thanks sottwell.

            • I think that the problem is that the definition of "empty" may not be what we think it is. TVs aren't "empty"-empty; an empty string isn't always considered "empty". It's one of those programming things that perhaps a formal education in CS would have helped me to understand better. Mostly I just fumble around with trial-and-error until something works.
                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
                • 47247
                • 4 Posts
                creatingvibrance Reply #7, 10 years ago
                I'm coming from a WordPress background and I realized that there are advantages and disadvantages between this two CMS. There are points that ModX is easy to understand because it's just been three days since I've seen the four tutorial videos and I'm almost done with my site. But some variables in WordPress is easy to understand (memorize).

                For example if testing for the featured image in wordpress its: if( (has_post_thumbnail) ) which is exactly what I needed.