Jump to content

'global' not working in templates


winston
 Share

Recommended Posts

Hello,

I am having trouble with the "global" keyword used in templates. What am I missing? Something obvious, probably.

Make this into a file and put it in the template directory:

<?
$cd = "this is a test";
roo();
function roo() {
    global $cd;
    print "[".$cd."]";
    }

It works if you run it from the command line (using php -f), but the $cd variable is not passed when the file is used as a template - it just prints the brackets.

Thanks for your help! Winston

Link to comment
Share on other sites

I don't think there is technically any need to have global variables, and you are better off avoiding them. But on the rare occasions where it seemed worthwhile, I'd usually use the $GLOBALS PHP superglobal anytime I referred to it, just to be really clear about what it was. i.e. 

$GLOBALS['cd'] = 'this is a test';
function roo() {
  print $GLOBALS['cd'];
}

That makes use of any global variables stick out like a sore thumb, reducing the potential confusion that comes from using global variables. 

Btw, the reason why just defining $cd outside the function doesn't make it a global is because ProcessWire templates are actually running from the namespace of a function in the TemplateFile class, not from the global namespace. 

  • Like 2
Link to comment
Share on other sites

  • 3 weeks later...
Thanks, and sorry for not getting back to this thread sooner. Jonathan's suggestion worked, and Ryan's reply was enlightening.
 
I had a feeling ignorance was the problem. Obviously I had some catching up to do on PHP namespaces.

I also had a nagging feeling of what Ryan said, that you are better off avoiding globals. Eventually I rewrote the code to avoid them.

Thanks again for your help. I am new to this community, and relatively new to Processwire, and I am very impressed by both.

  • Like 2
Link to comment
Share on other sites

  • 3 years later...

While it's generally a good idea to avoid globals in PHP I'm wondering what is the easiest way to access the PW API variables like $page, $user, $fields etc. from a function body within a template file. I would like to understand them as a kind of "superglobals", so I find it tediuos to pass them as parameters to every function that needs them. On the other hand, I haven't found a solution to access them as PHP globals within a function (with my humble PHP knowledge). Neither Jonathan's solution nor the $GLOBALS approach seems to work with PW "globals".

Are there any best practices how to acces the PW API variables from a function?

Link to comment
Share on other sites

5 minutes ago, FlorianA said:

I'm wondering what is the easiest way to access the PW API variables like $page, $user, $fields etc. from a function body within a template file. I

wire('page');
wire('user');
wire('fields');
wire('pages');
// etc...

 

  • Like 4
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...