We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 32963
    • 1,732 Posts
    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;


      xWisdom
      www.xwisdomhtml.com
      The fear of the Lord is the beginning of wisdom:
      MODx Co-Founder - Create and do more with less.
      • 33337
      • 3,975 Posts
      Great tip !

      Thanks for sharing !

      Regards,

      zi
        Zaigham R - MODX Professional | Skype | Email | Twitter

        Digging the interwebs for #MODX gems and bringing it to you. modx.link
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        Yeah, I found out the hard way (with a [*language*] tv) that you can’t use a tv in the snippet code; you have to pass it as an arguement.
          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
          • 14868
          • 17 Posts
          first post here. any help would be appreciated.

          I can’t seem to properly pass the template variable to my snippet inside a chunk. I know the tv works b/c i display it before my snippet call, but once inside the snippet i can’t access the value.

          here’s the code below:

          <p>[*statusCode*]</p>
          [[routeReservationRequest? &currentStatus=`[*statusCode*]` ]]

          The tv value shows in the chunk, but when i echo the value in the snippet it’s empty. I tried to initially just access the tv from within the snippet but not sure how to do that either.

          any help is appreciated.

          side point: can anyone point me to a good reference on the scope of template variables within modx?
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            Try the snippet uncached, [!...!]
              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
              • 14868
              • 17 Posts
              genius! that did the trick.
                • 8085
                • 4 Posts
                Of course you can do it.

                $tv_value = $modx->getTemplateVarOutput(’your_var’);
                echo ($tv_value[’your_var’]);
                  • 3749
                  • 24,544 Posts
                  Or in Revo:

                  <?php
                  $id = $modx->resource->get('id');
                  $tv = $modx->getObject('modTemplateVar','TV_name');
                  $output = $tv->renderOutput($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
                    • 5274
                    • 177 Posts
                    Quote from: BobRay at Aug 24, 2009, 07:03 PM

                    $tv = $modx->getObject('modTemplateVar'),'TV_name');
                    


                    Is that a typo or are open "()" allowed in Revo?
                      • 3749
                      • 24,544 Posts
                      Quote from: Chuck at Aug 24, 2009, 09:32 PM

                      Quote from: BobRay at Aug 24, 2009, 07:03 PM

                      $tv = $modx->getObject('modTemplateVar'),'TV_name');
                      


                      Is that a typo or are open "()" allowed in Revo?

                      Revo is smart enough to correct any possible error in your php code. In fact, you can leave the PHP code sections completely blank and Revo will create them for you. wink

                      Seriously, thanks for catching that. I was in a hurry when I dashed it off. Fixed in the post above (I hope).
                        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