We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 32391
    • 37 Posts
    Hi there,

    First time using modx and absolutly loving it, few noob issues though!

    My site has a main image on almost every page. I’ve created a tv to add it to the template. If this tv is not set, I don’t want the img tag to display.
    I’ve tried this

    [*mainImage:isnot=``: then=`<img id="mainImage" style="margin:0; padding:0;" src="[*mainImage*]" width="541" height="285" alt="" />`*]

    but it doesn’t work. It just outputs:
    width="541" height="285" alt="" />`*]
    as text.
    Can anyone tell me how to get this to work or suggest an alternative?

    Thanks in advance!
      • 15082
      • 195 Posts
      Not sure but try to remove the inline styles for testing. I’m guessing the ":"s can confuse PHx...

      EDIT: I would also try to remove the space between ":" and "then=".

      Greetz
      JUSI
        • 36926
        • 701 Posts
        Quote from: Wilbo at Sep 30, 2009, 05:12 AM

        Hi there,

        First time using modx and absolutly loving it, few noob issues though!

        My site has a main image on almost every page. I’ve created a tv to add it to the template. If this tv is not set, I don’t want the img tag to display.
        I’ve tried this

        [*mainImage:isnot=``: then=`<img id="mainImage" style="margin:0; padding:0;" src="[*mainImage*]" width="541" height="285" alt="" />`*]

        but it doesn’t work. It just outputs:
        width="541" height="285" alt="" />`*]
        as text.
        Can anyone tell me how to get this to work or suggest an alternative?

        Thanks in advance!

        You have installed phx right? just checking.

        Try this

        [*mainImage:is=``:then=`` :else`<img id="mainImage" style="margin:0; padding:0;" src="[*mainImage*]" width="541" height="285" alt="" />`*]

        Or can’t you just make the TV and image widget?

        Ben
          • 32391
          • 37 Posts
          I hadn’t intsalled phx!

          Thanks for your help. I the end I did it with a snippet instead.

          Thanks again.
            • 40045
            • 534 Posts
            Can I ask how you did it? I’m also very new to ModX (lovin it =D) and I also have some TVs I don’t want to display if they’re not filled out.


            Thanks for your answer!
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              Here’s a snippet I use to load a Flowplayer flash player if a valid movie file is in the TV.
              <?php
              $movie = $modx->getTemplateVar('video');
              if (!empty($movie['value'])) { // TV field is not left empty
                  if(is_file('assets/flash/videos/pages/' . $movie['value'] . '.flv')) { // movie file actually exists
                       $chunk = $modx->getChunk('rightflash'); // chunk has HTML for loading player
                       return $chunk;
                  }
              }
              return;
              ?>

              You could easily add an else {...} section to do something else if the TV is empty.
                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
                • 40045
                • 534 Posts
                @sottwell

                thanks for your snippet, I may use this!

                I also found another solution (don’t know if this would work with embedding flash, but it should I think) via PHx...worked the first time with it and it’s beautiful =)...

                [+phx:if=`[+cfm_article_link+]`:is=``:then=`<span>[+title+]</span>`:else=`<span><a href="[+cfm_article_link+]">[+title+]</a></span>`+] 


                could also be

                [+cfm_article_link:is=``:then=`<span>[+title+]</span>`:else=`<span><a href="[+cfm_article_link+]">[+title+]</a></span>`+]


                they do both the same (both examples are used with Ditto --> syntax)

                if you use it on a template it will look a bit different, like this:

                [+phx:if=`[*cfm_article_link*]`:is=``:then=`<span>[*pagetitle*]</span>`:else=`<span><a href="[*cfm_article_link*]">[*pagetitle*]</a></span>`+]



                I think, this is a far more elegant way to achieve such things, because if you do it with snippets you have to make (or I would have to make, because of laking PHP skills) a snippet for every TV you need a if/else statement...