winston Posted October 12, 2013 Share Posted October 12, 2013 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 More sharing options...
Jonathan Dart Posted October 12, 2013 Share Posted October 12, 2013 Try making $cd explicitly global. <? global $cd; $cd = "this is a test"; roo(); function roo() { global $cd; print "[".$cd."]"; } 1 Link to comment Share on other sites More sharing options...
ryan Posted October 16, 2013 Share Posted October 16, 2013 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. 2 Link to comment Share on other sites More sharing options...
winston Posted November 5, 2013 Author Share Posted November 5, 2013 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. 2 Link to comment Share on other sites More sharing options...
FlorianA Posted July 30, 2017 Share Posted July 30, 2017 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 More sharing options...
kongondo Posted July 30, 2017 Share Posted July 30, 2017 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... 4 Link to comment Share on other sites More sharing options...
adrian Posted July 30, 2017 Share Posted July 30, 2017 Another option is the functions API: https://processwire.com/blog/posts/processwire-3.0.39-core-updates/#new-functions-api which lets you do things like: pages("template=basic-page") 4 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now