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

    i got a Snippets on my site which i want to execute depending if a TV is filled or not.

    right now i got this solution:
    <?php
    $photos = $modx->getTemplateVar('photos');
    if (!empty($photos['value'])) {
             $SnippetCall = '{{SnippetCall}}';
             return $SnippetCall;
    }
    return;
    ?>

    "photos" is the TV which i want to be checked, and the Chunk "SnippetCall" contains, well, yes, the actual Snippet call. The Value of the TV is used for the Snippet output.

    This works, but is it the right approach? (e.g. preformance-wise) btw. Evo 1.0.5

    thanks, j
      • 4310
      • 2,310 Posts
      I think integrating this snippet into the SnippetCall snippet would be more efficient/cleaner overall.
        • 26931
        • 2,314 Posts
        I think integrating this snippet into the SnippetCall snippet would be more efficient/cleaner overall.
        uhm, of course rolleyes just overcomplicated things. again. thanks smiley
        weird , i get one more request on the uncached resource and 2 additional on the cached
          • 1122
          • 209 Posts
          <?php
          $photos = $modx->getTemplateVar('photos');
          if (!empty($photos['value'])) {
              return $modx->runSnippet('your-snippet-name', array('param_1' => $value1, 'param_2' => $value2, ...));
          }
          ?>

          and we are saving at least two runs of MODx parser.
            • 26931
            • 2,314 Posts
            thank you alik, awesome smiley

            but wouldn’t it be the same to put something like:
            <?php
            $LatLng = $modx->getTemplateVar('mkrLatLng');
            $KML = $modx->getTemplateVar('KML');
            if (empty($LatLng['value']) && empty($KML['value'])) {
                     return '';
            }
            ?>
            

            at the beginning of the Snippet i want to execute?
              • 1122
              • 209 Posts
              Yes, it will be the same.

              (Your condition "empty && empty" means that the snippet will be executed provided that at least one TV is filled-in. If your intention is to execute the snippet when both TVs are filled-in, use "empty || empty".)
                • 3749
                • 24,544 Posts
                BTW, you are testing the default value of the TV with that code. If you want to get the value for the current resource, you’d want this (no need to get the TV object):

                $modx->resource﷓>getTVValue('TvName');


                or

                $modx->resource﷓>getTVValue($id); /*  where $id is the ID of the TV -- faster */  
                  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
                  • 26931
                  • 2,314 Posts
                  Hi Bob, thanks ... Evo or Revo smiley this is for Evo
                    • 3749
                    • 24,544 Posts
                    Quote from: sharkbait at Feb 13, 2011, 02:29 AM

                    Hi Bob, thanks ... Evo or Revo smiley this is for Evo
                    Oops. I should have seen that. My code was for Revo. tongue
                      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