We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 11055 ☆ A M B ☆
    • 3,112 Posts
    This may be a noob question, but I’ve searched this forum and nothing fit.

    My include file (snippet.inc.php):
    <?php
    include_once $modx->config['base_path'].'template.php';
    
    $a='a variable';
    template(); // I'm trying to display the result
    ?>
    


    My template function (template.php):
    <?php
    function template(){ // This was meant to be a display function
      global $a;
      echo 'I\'m calling '.$a;
    }
    ?>
    


    If I run this code outside of modx, it works.

    I’m calling a variable // test outside modx
    I’m calling // result in modx

    How to make it run inside modx?
      Rico
      Genius is one percent inspiration and ninety-nine percent perspiration. Thomas A. Edison
      MODx is great, but knowing how to use it well makes it perfect!

      www.virtudraft.com

      Security, security, security! | Indonesian MODx Forum | MODx Revo's cheatsheets | MODx Evo's cheatsheets

      Author of Easy 2 Gallery 1.4.x, PHPTidy, spieFeed, FileDownload R, Upload To Users CMP, Inherit Template TV, LexRating, ExerPlan, Lingua, virtuNewsletter, Grid Class Key, SmartTag, prevNext

      Maintainter/contributor of Babel

      Because it's hard to follow all topics on the forum, PING ME ON TWITTER @_goldsky if you need my help.
      • 10487 MODX Staff
      • 1,535 Posts
      Garry Nutting Reply #2, 17 years ago
      It’s a question of variable scoping (remember that snippets are eval’ed within MODx Evo), this works:
      <?php
      global $a;
      
      function template(){ // This was meant to be a display function
        global $a;
        return 'I\'m calling '.$a;
      }
      
      $a = 'a variable';
      return template($a);
      ?>
      

      Although, the more preferable way (to eliminate globals) would be:
      <?php
      function template($a){ // This was meant to be a display function
        return 'I\'m calling '.$a;
      }
      
      $a = 'a variable';
      return template($a);
      ?>
      
        Garry Nutting
        Senior Developer
        MODX, LLC

        Email: [email protected]
        Twitter: @garryn
        Web: modx.com
        • 11055 ☆ A M B ☆
        • 3,112 Posts
        hummm...
        the global’s needed because we’re talking about 2 different files, no?

        Anyway,
        <?php
        $a = 'a variable';
        return template($a);
        

        not run

        <?php
        $a = 'a variable';
        echo template($a);
        

        run

        How should I use return?

        Meanwhile, this one work:
        file: snippet.inc.php
        <?php
        global $a; // different with a standalone php file
        $a = 'a variable';
        return template();
        ?>
        
        file: template.php
        <?php
        function template(){ // This was meant to be a display function
          global $a;
          echo 'I\'m calling '.$a;
        }
        


        Or
        file: snippet.inc.php
        <?php
        global $a; // different with a standalone php file
        $a = 'a variable';
        echo template(); // I think this one, since the "echo" doesn't immediately end execution the rest of the codes.
        ?>
        
        file: template.php
        <?php
        function template(){ // This was meant to be a display function
          global $a;
          return 'I\'m calling '.$a;
        }
        

          Rico
          Genius is one percent inspiration and ninety-nine percent perspiration. Thomas A. Edison
          MODx is great, but knowing how to use it well makes it perfect!

          www.virtudraft.com

          Security, security, security! | Indonesian MODx Forum | MODx Revo's cheatsheets | MODx Evo's cheatsheets

          Author of Easy 2 Gallery 1.4.x, PHPTidy, spieFeed, FileDownload R, Upload To Users CMP, Inherit Template TV, LexRating, ExerPlan, Lingua, virtuNewsletter, Grid Class Key, SmartTag, prevNext

          Maintainter/contributor of Babel

          Because it's hard to follow all topics on the forum, PING ME ON TWITTER @_goldsky if you need my help.