We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 21122
    • 153 Posts
    Ok, I’m using the modX CMS on lots of sites now. I love it. Its groovy.

    I have a little issues that I’d like sorted though. For each news item, I’m using a template (with Template Variables) which spits out the headline, then the date, then the content, and then any file thats been uploaded with the story (a newsletter for example).

    The problem I have is, when the user doesn’t upload any file, it still adds a little download icon (which I have added to the tag with a background image in the CSS). I was wondering if there was a nice little script I could add which removed the icon if no content was uploaded.

    So if there is an upload, the markup is something like:

    <a href="linktouploadedfile.pdf" class="download">Name of uploaded file</a>
    


    and if there is no file uploaded, the code would be:

    <a href="" class="download"></a>
    


    So I guess if thats the case, I just want to remove the whole link, or just have it remove the background image.

    Any ideas?

    Thank you
      • 27708 MODX Staff
      • 2,502 Posts
      This is a time for PHx (Placholders Extended). PHx is a MODx plugin that lets you perform operations on output: http://wiki.modxcms.com/index.php/PHx . Installing PHX and then I add the custom modifier called "isnotempty" which allows you to display content only if a TV (or Document Variable) if it is not empty.

      You can find the usage here: http://wiki.modxcms.com/index.php/PHx/CustomModifiers#phx:isnotempty but basically you just install the phx:isnotempty snippet:

      <?php
      $condition[] = intval(trim($output) != '');
      ?>
      


      and then call it [+myplaceholder:isnotempty:then=`String to return if not empty`+]
        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. Blog ✦ Twitter ✦ LinkedIn ✦ GitHub
        • 21122
        • 153 Posts
        Thanks for that SR. It looks like that will do what I want it to.

        So now for the implementation!

        I’ve installed PHx, and also the phx:isnotempty as a snippet called ’strip’ (I presume this is te correct way to do it?)

        In my chunk for the news articles, I have the following:

        <div class="news-archive">
        <h4><a href="[~[+id+]~]">[+title+]</a></h4>
        <p class="date">[+date+]</date>
        <p>[+summary+]</p>
        <p><a class="download" href="[+file-attachment+]">[+attachment-title+]</a></p>
        <p><a href="[~[+id+]~]" class="read-more">Read More »</a></p>
        </div>
        


        If an attachment has been uploaded to the news item, it will populate those fields (file-attachment, and attachment-title). So I don’t want that whole line from the opening <p> tag to the closing </p> tag to appear if no file has been uploaded.

        So, any clues how I’d implement this using the PHx thingy?

        Thanks
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Ditto has its own PHx; no need to install the plugin.

          [+file-attachment:isnotempty:then=`<p><a class="download" href="[+file-attachment+]">[+attachment-title+]</a></p>`+] 
            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
            • 21122
            • 153 Posts
            Thank you so much for your help Susan.

            Predictably, that worked like a charm!

            So just to get this straight, I didn’t need to have PHx or the ’isnotempty’ snippet installed?

            Thanks again!
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              You didn’t need to install the PHx plugin. The snippet you need, since it extends the Ditto PHx as well.

              The Ditto plugin is just the PHx class file included directly by Ditto and used on its own placeholders. The plugin is used on all placeholders in the page, whether from Ditto or not. I prefer not to use the plugin; it’s very seldom that a snippet or TV wouldn’t do the job just as well, and skip the overhead of the plugin pre-parsing your document.
                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
                • 21122
                • 153 Posts
                Beautifully explained!

                Thanks for all your help. PHx has now been deleted!

                Cheers