We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 11093
    • 50 Posts
    Hi all,
    I’ve a problem, there it is :
    I create a nippet with this code :

    $uid = 2
    function get_profil() {
       global $modx, $uid;
       return $uid;
    }
    echo get_profil(); 


    Then, i load my snippet :
    [!mySnippet]]


    Simple ^^ However, my snippet return empty, the global in my function doesn’t work ?
    Has someone any idea ? (sorry for my english ;p)
      • 27708 MODX Staff
      • 2,502 Posts
      Try this.

      <?php
      $uid = 2;
      return($uid);
      ?>


      You could certainly build it around a function but you always need to return a value string, or array as the output of the snippet not an echo statement.

      Not to mention your snippet call should be as follows [!mysnippet!] or [[mysnippet]] not [!mysnippet]]
        Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup magician. Blog ✦ Twitter ✦ LinkedIn ✦ GitHub
        • 11975
        • 2,542 Posts
        Hi

        there a couple of errors in your sample coade and snippet call


        <?php

        $uid = 2 // ; is missing
        function get_profil() {

        and snippet call are [[... means cached call...]] and [!... means uncached...!]
        you ’re mixing the syntax starting with a [! and closing by ]] so it cannot work.

        :-)
          Made with MODx : [url=http://www.copadel.com]copadel, fruits et l
          • 11093
          • 50 Posts
          ok, i tried like this :

          $uid = 2;
          function get_profil() {
             global $modx, $uid;
             return $uid;
          }
          return get_profil(); 


          Then, i load my snippet :
          [!mySnippet!]


          My snippet is still return me nothing :/
          ps smashingred : You remove my function, this code is an exemple. I need my function :/

          My Modx Version is 0.9.6.2
            • 11975
            • 2,542 Posts
            You need to declare $uid as a global outside the function

            global $uid;
            $uid = 2;

            function() {
            global $uid;


            } ....


            :-)
              Made with MODx : [url=http://www.copadel.com]copadel, fruits et l
              • 11093
              • 50 Posts
              Ok it works, great.
              Really thanks, i close my post

              Ps : heliotrope : Calling a snippet with [!...]] works, i don’t know if it’s recommended, but it works like [!....!].
                • 25663 MODX Staff
                • 12,272 Posts
                [! means it’s uncached and evaluated every time the page is visited. It’s not only recommended, but required in this case. smiley
                  Ryan Thrash, MODX Co-Founder
                  Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me