We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7072 ☆ A M B ☆
    • 43 Posts
    How can I import variables from one snippet into another?

    e.g. I have $variable1 = ’Value1’; in [[MyVariables]] and i want to import this variable into [[MyOtherSnippet]]
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      You may simply be able to declare it global; I’m not exactly sure which one or both would you have to declare it global. You can load it into the $_SESSION. You can load it into $_GLOBALS. You can use $mdox->setPlaceholder(’myvar’, ’value’) in the first snippet, and $modx->getPlaceholder(’myvar’) in the second snippet (or use it as a parameter with [+myvar+]). This is MODx. There’s probably a dozen ways you can do that.

      On checking it out, it turns out you need to declare it global in both snippets.

        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
        • 4172
        • 5,888 Posts
        for global sitewide-variables I tend to use one modx-resource as ’settings’ where I can set this global vars as TVs.

        you can then read them on all places where you need them with

        <?php
        $settingTVs='variable1,variable2,variable3,variable4';//settingTVs to read
        $settingid='99';//resource-ID of setting-resource 
        
        $settingTVs=explode(',',$settingTVs);
        $tvs=$modx->getTemplateVarOutput($settingTVs,$settingid);
        echo '<pre>'.print_r($tvs,1).'</pre>';
        

          -------------------------------

          you can buy me a beer, if you like MIGX

          http://webcmsolutions.de/migx.html

          Thanks!
          • 7072 ☆ A M B ☆
          • 43 Posts
          Quote from: sottwell at Jul 14, 2010, 07:13 AM

          You may simply be able to declare it global; I’m not exactly sure which one or both would you have to declare it global. You can load it into the $_SESSION. You can load it into $_GLOBALS. You can use $mdox->setPlaceholder(’myvar’, ’value’) in the first snippet, and $modx->getPlaceholder(’myvar’) in the second snippet (or use it as a parameter with [+myvar+]). This is MODx. There’s probably a dozen ways you can do that.

          On checking it out, it turns out you need to declare it global in both snippets.



          Thank you Susan, this was exactly what i needed... And it is so simple that i wonder why i didn’t think of it myself smiley