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

    I know this is a noob question but I haven't managed to find a solution.
    I'm using MODX 2.2.9-pl
    I have a TV generated by "Simple File Upload TV" http://modx.com/extras/package/simplefileuploadtv
    The output of the TV is set to 'default'.

    On some pages the client may choose to pick a photo, on other pages they may choose not to, so I wanted to write a snippet that renders the <img> tag only when there is a photo selected.

    Because it failed to work like expected, I tried a simple test snippet:

    $myphoto= '[[*Photo1]]';
    if (!empty($myphoto)){
    	echo('This is the file: '.$myphoto);
    }else if(empty($myphoto)){
    	echo('Empty');
    };


    When I have a photo selected, I see the correct output: "This is the file: images/properties/_prop25-10.Island-Villas-2.jpg "
    However, when I have no photo selected, instead of rendering the string "Empty", I get "This is the file: "
    I don't know how to go about it. I would appreciate any help smiley

    I have also tried:
    if ($myphoto=='')
    and again it does not seem to match with the situation where no photo is selected.
      • 4172
      • 5,888 Posts
      try:

      $myphoto= $modx->getOption('myphoto',$scriptProperties,'');
      return !empty($myphoto) ? 'This is the file: '.$myphoto : 'Empty';
      


      and call that snippet with:

      [[!yourSnippet? &myphoto=`[[*Photo1]]`]]
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 4583
        • 24 Posts
        Hi Bruno,

        I tried it, and it returns "This is the file: " when there's no photo selected, and "Empty " when there is a photo selected, so it's the other way around and it doesn't output the value of the TV, but it clearly tells the difference between the empty and not empty TV.

        I'm curious why my script work didn't? I would rather just call the snippet without passing any values to it.
          • 4172
          • 5,888 Posts
          I did edit my Post and added the ! to emtpy($myphoto)

          this
          $myphoto= '[[*Photo1]]';


          is never empty, because it is not parsed, when inside a snippet.
          you can see the TV-value, because your snippet is outputting the placeholder:
          [[*Photo1]]

          which after that is parsed by modx.

          If you don't want to pass a value to your snippet, try:

          $myphoto= $modx->resource->getTVValue('Photo1');
          return !empty($myphoto) ? 'This is the file: '.$myphoto : 'Empty';


            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 4583
            • 24 Posts
            Last one worked like a charm! Thank you Bruno17 ;-)