We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26968
    • 25 Posts
    Perhaps you have already answered this question in this posting, but at the risk of seeming dense:

    How do you pass a template variable to a snippet, so that a modx content editor can change a variable on the fly? For instance, lets say I have a snippet that creates a php generated gallery and uses a declared variable $dir = ’assets/images/directory’; to the directory location where the images are stored. How would I pass that template variable to the snippet at runtime?

    Template Variable name = locationTV

    In the snippet, is it:
    $dir = $modx->getObject(’modTemplateVar’,’locationTV’);

    just trying to clarify, please advise, thanks.
      • 3749
      • 24,544 Posts
      Quote from: shawntorres at Jan 28, 2010, 10:06 PM

      Perhaps you have already answered this question in this posting, but at the risk of seeming dense:

      How do you pass a template variable to a snippet, so that a modx content editor can change a variable on the fly? For instance, lets say I have a snippet that creates a php generated gallery and uses a declared variable $dir = ’assets/images/directory’; to the directory location where the images are stored. How would I pass that template variable to the snippet at runtime?

      Template Variable name = locationTV

      In the snippet, is it:
      $dir = $modx->getObject(’modTemplateVar’,’locationTV’);

      just trying to clarify, please advise, thanks.

      The easy way would be:

      [[!snippetName? &directory=`[[*locationTV]]` ]]


      The TV would then be available in the snippet as $directory.

      If you prefer to do it in the snippet, you’re on the right track, but the second argument needs to be an array and for TVs, you need to tell MODx which document you want the TV value from:

      <?php
      $dirTV = $modx->getObject('modTemplateVar',array('name'=>'locationTV'));
      
      $dir = $dirTV->getValue($docID); // docId could be sent as an argument in the snippet tag: &docID=`[[*id*]]`.
      ?>




        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
        • 26968
        • 25 Posts
        Thanks Bobray,

        The easy way worked like a charm. Set up a lightbox gallery with a TV (text type - no widget) called [*pictures_location*]

        set up template with call to lightbox js.

        In the template, used the following call:
        [[Gallery? &path=`[*pictures_location*]` ]]

        Credit for the bulk of snippet goes to KYOKUTYO, http://kyokutyo.com/
        http://snipplr.com/view/5527/display-pictures-in-the-directory/

        Below is the modified snippet if anyone is interested:

        <?php
        $fileTypes = array(’jpg’,’jpeg’,’gif’,’png’);
        $width = 100;
        $height = 100;
        $pageTitle = "Pictures";
        // *************************************************************
        $f = join(’,*.’, $fileTypes);
        $f = ’*.’.$f;
        $height += 20;
        ?>

        <style type="text/css">
        <!--

        .gallery ul { margin-top:10px; list-style:none; }

        .gallery ul li { width:<?php echo $width; ?>px; height:<?php echo $height; ?>px; float:left; padding:5px; overflow:hidden; margin:0px; }

        .gallery ul li span { height:0px; font-size:medium; font-style:italic; }

        .gallery ul li a img { width:<?php echo $width; ?>px; height:75px; border:dotted 1px #ddd; }

        -->
        </style>

        <div id="columnwrap">
        <div style="padding-left:20px;padding-right:20px;">

        <div class="gallery" style="display:block; width:100% clear:both;">
        <ul>

        <?php $loop = 1; foreach (glob("$path{".$f."}", GLOB_BRACE) as $fileName) { ?>

        <li>

        <a href="<?php echo $fileName; ?>" rel="lightbox[gallery]"><img alt="<?php echo $fileName; ?>" src="<?php echo $fileName; ?>" /></a>

        </li>

        <?php $loop++; } ?>
        </ul>
        </div><!-- END GALLERY-->
        ?>

        and here is the test page:
        http://h2ksports.com/index.php?id=125

        cheers,
        s.

          • 3749
          • 24,544 Posts
          Glad it worked. laugh

          Thanks for sharing your snippet.
            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
            • 10940
            • 71 Posts
            I cant seem to get a RichText TV to work as a snippet parameter.


            I can get this exact code to work with plain text tvs. The only thing I can think of is you cannot use a rich text area as a tv snippet parameter. Can anyone let me know if this is the case?



            Snippet call:

            [[removeSpaces? &right=`[*rightcolumn*]`]]


            Snippet:

            <?php
            $right = isset($right) ? $right : ’’;

            // Allow <img> and <a>
            $output = strip_tags($right, ’<img><a>’);
            return $output;
            ?>



            What it outputs on the website front-end:



            [[removeSpaces? &right=`

            ****tv prints out here and has not been parsed by snippet as it still contains tags other than <a> and <img>*****

            `]]
            • What happens if you have the snippet uncached [!removeSpaces...!]
                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
                • 1303
                • 3 Posts
                Note: passing TVs does not work with Image (widget)-type TVs. Took me 30mins to figure it out.
                  • 17775
                  • 9 Posts
                  Quote from: xwisdom at Aug 09, 2005, 08:33 PM

                  It’s very easy to pass the value of a Template Variable to a Snippet.

                  For example, you could create a Template Variable (text input) called BGColor and pass the value entered by the user into a Snippet as follows:

                  [[MySnippet? &color=`[*BGColor*]`]]

                  When the Parser executes it will then output the value of the [*BGColor*] variable into the snippet.

                  MySnippet Code:
                  echo 'You have entered: '.$color;




                  I’m having difficulty in applying HTMLENTITIES with a snippet return call.

                  Example Code:
                  Template variable: "Black & White"

                  echo 'You have entered: '.htmlentities($color);


                  It should return "Black &amp White", but displays "Black & White";


                  • Are you looking at the source of the display, or at the visible display in your browser?
                      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
                      • 17775
                      • 9 Posts
                      Quote from: sottwell at Jul 08, 2010, 10:08 AM

                      Are you looking at the source of the display, or at the visible display in your browser?

                      I’m looking at the source code. The HTML entities does seem to work when you pass a TV to a Snippet.