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

    First please excuse my ignorance. I’m new to MODx (been using it a little over 2 weeks now), I’m also not very savvy in PHP. So far MODx is great and I’m relay enjoying learning it. I have a web site I’m working on that has two columns, one with content and the other with images. Up until now I have just be creating many different templates depending on how many images I want in the image column, but this isn’t very clean and adds an extra step for the end user (choosing the correct template). Also now I’m trying to create an image gallery template and same as my image columns I don’t want to create multiple templates depending on how many images I want.

    So today I decided I would try write a simple snippet that checks my template variables, and if they are null ignores them, but if they have a value it would echo the formating code I want. I tried this code:

    <?php
    /* Set up the PHP variables with template variable values */
    $image_1 = [*galleryimage1*];
     
    if ( $image_1 == null ) {
        echo "";
    } else {
        echo "<li><img src="[*galleryimage1*]" alt="[*galleryalt1*]" /></li>";
    }
    ?>


    Unfortunately this dousn’t work, I’m assuming MODx has some special syntax for using TVs and such in Snippets. Like I mentioned I’m a noob at both PHP and MODx, so any help you can give would be great. I have spent a lot of time searching the web, these forms and the documentation but haven’t found anything that answers my questions. Thanks in advance - Jeff
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      If you have the image TVs using the image output widget, you can just use the TV tags and it won’t put out anything if it’s 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
        • 11449
        • 28 Posts
        Ok got help on the IRC. I had a number of problems. Here are two ways to do this:

        Option 1

        Snippet Call
        [[gallerymaker]]


        Snippet Code
        <?php
        /* Set up the PHP variables with template variable values */
        $image_1 = $modx->documentObject['galleryimage1'];
        $alt_1 = $modx->documentObject['galleryalt1'];
         
        if ( $image_1 == null ) {
            echo "";
        } else {
            echo "<li><img src=\"$image_1\" alt=\"$alt_1\" /></li>";
        }
        ?>
        


        Option 2

        Snippet Call
        [[gallerymaker? &alt_1=`[*galleryalt1*]` &image_1=`[*galleryimage1*]`]]


        Snippet Code
        <?php
         
        if ( $image_1 == null ) {
            echo "";
        } else {
            echo "<li><img src=\"$image_1\" alt=\"$alt_1\" /></li>";
        }
        ?>
        
          • 29201
          • 239 Posts
          I’ve tried to implement this but it appears that my snippet isn’t getting the values that are stored in my template variable.

          For me I want to simply display "something" if there is a value present for the Template Variable restaurant_image_link.

          First, I created the template variable "restaurant_image_link" with a type URL.

          Then created my snippet, named "gallerymaker":

          <?php
          /* Set up the PHP variables with template variable values */
          $restaurant_image_link = $modx->documentObject['restaurant_image_link'];
           
          if ( $restaurant_image_link == null ) {
              echo "null";
          } else {
              echo "something";
          }
          ?>


          Then I call the snippet in a Chunk as follows:

          [[gallerymaker]]

          I’ve tried cached and unccached and I get the same result which is:

          "null".

          I’d really like it to display "something".....and I have verified that I do have URLs stored in the TVs.




            • 3749
            • 24,544 Posts
            I would recommend Option two (above). Send the TVs in the snippet call. You could also send the "something" in your snippet call as well. That would make the snippet much more generic and keep you from having to hard code the TV names into the snippet.

            [[gallerymaker? &alt_1=`[*galleryalt1*]` &image_1=`[*galleryimage1*]` &empty_message=`Something else here`]]

            <?php
            if (! empty ($image_1)) {
                $output = '<li><img alt="' . $alt_1 . '"src="' . $image_1 . '"></li>';
            } else {
                $output = &empty_message;
            }
            return $output;
            ?>


            Be careful with the spelling of your snippet, TV, PHP variable and parameter names -- they’re case-sensitive. If you have trouble, be sure to do a "view source" on the page so you can see exactly what the snippet is returning.

            BTW, if you’re not comfortable with PHP, you could do this with PHX. With the PHX plugin installed, this would do the same thing (something like this):

            [*galleryimage1:is=``:then=`something else -- TV is empty`:else=`"<li><img src="[*galleryimage1*]" alt="[*galleryalt1*]" /></li>"` *]
              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting