We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 25483
    • 741 Posts
    I was wondering if it is possible to use 1 template variable which is able to choose a image and is able to set a link for the selected image? I also want to be able to include no link when it is not needed.

    I could just use 2 templates variables, 1 for the image and 1 for the link. But it should be easier to do with 1 TV. And I don’t know how to check if there is a link for the image? So that I can stripout the link part if there is no link... I think I need to look at PHx??

    Can somebody help me with this?
      with regards,

      Ronald Lokers
      'Front-end developer' @ h2o Media

    • @mediaguy I’ve done similar things recently and what you want to do is build a snippet-widget to style the output of the TV.

      I assumed you wanted to link to the image and not to a document. If you want to link to a document the following won’t be necessary and you can just style it in the template. BUT if you are wanting to link to the image itself, here you go:

      I’d only use one TV and make it an image widget. I try to make it so clients never have to think about what they are doing. There is a tutorial in the public wiki but I’ll explain it here since it helps me remember these things as this is a very powerful little method.

      So create your TV and make it of the type image. But don’t put the TV call in the template. For the sake of this I’ll use the TV name "img_tv".

      Then create a snippet to get the contents of the TV like so:

      <?php
      $tvArray = $modx->getTemplateVarOutput(array('img_tv'));
      
      $img = $tvArray[img_tv];
      
      if (empty($img)){
      	$output = '';
      }
      else {
      	$output = '<a href="'.$img.'"><img src="'.$img.'" /></a>';
      }
      return $output;
      ?>
      

      Name the snippet img_tv_snip.

      Place your snippet call in your template [[tv_img_snip]].

        Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup magician. BlogTwitterLinkedInGitHub
        • 25483
        • 741 Posts
        Nice example shocked, but indeed I want to link to a document. I think I need to learn a little bit more about PHx to create what I want smiley
          with regards,

          Ronald Lokers
          'Front-end developer' @ h2o Media

        • Yeah, PHx will do what you want. You will need 2 tvs for sure then. PHx is very easy to understand once you play with the conditional logic syntax.

          Have fun.
            Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup magician. BlogTwitterLinkedInGitHub
            • 7231
            • 4,205 Posts
            If you want the browse button with the resource editor then you will need 2 TVs (one for each media) then use either a snippet of PHx to output the correct one.
              [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

              Something is happening here, but you don&#39;t know what it is.
              Do you, Mr. Jones? - [bob dylan]
            • I was wondering if you could do this without multiple template variables... just like how the TinyMCE gives you the Tree icon to select an image, then it generates the whole img tag, including height, width, and alt.