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

    I'm trying to set a snippet variable to a modx template variable which I can use in a function. In this case my template variable would be the string 'Brandname'. Eventually I am trying to write a function using a switch statement that will dynamically replace content based on the template variable string. Right now I can't get the template variable to be read as a string. Here's what's not working:

    $myBrand = '[[*Brand]]';
    
    if ($myBrand == 'Brandname'){
           echo 'hello';
    }


    But if I simply put:

    echo $myBrand;


    OR

    var_dump($myBrand);


    Then I will see that $myBrand has been set to 'Brandname' and am thus a little confused as to why it gets represented in the echo statement and var_dump but not in my simple 'if' statement.

    I am running ModX 2.2.8

    Thanks for your help!

      • 4172
      • 5,888 Posts
      well,

      $myBrand = '[[*Brand]]';
      echo $myBrand;


      echoes just

      [[*Brand]]


      and the MODX - parser replaces it with the value of the TV.

      there are two ways to get the value into your snippet:

      [[yourSnippet? &myBrand=`[[*Brand]]`]]


      Than you can use it inside your snippet with

      $output = '';
      if ($myBrand == 'Brandname'){
             $output = 'hello';
      }
      
      return $output;
      


      or better:
      $myBrand = $modx->getOption('myBrand',$scriptProperties,'');
      $output = '';
      
      if ($myBrand == 'Brandname'){
             $output = 'hello';
      }
      
      return $output;
      


      Or you use the API to get the TV - value with

      $myBrand = $modx->resource->getTVValue('Brand');
      if ($myBrand == 'Brandname'){
             $output = 'hello';
      }
      return $output;
      





      [ed. note: Bruno17 last edited this post 13 years, 2 months ago.]
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 3749
        • 24,544 Posts
        I know this looks wrong, but that should be $modx->resource->getTVValue() (three caps).
          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
          • 4172
          • 5,888 Posts
          I'm allways not sure about the correct casing and need to look into modresource.class.php before using it.

          corrected it above.
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 3749
            • 24,544 Posts
            The only reason I know is that I had it wrong in my book and had to fix it in a bunch of places. wink

              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