Godfrey Posted June 8, 2013 Posted June 8, 2013 Quick question: What, if any, is the difference between wire("page") and $page? I know that wire() provides you with a global variable, but it seems that if you are selecting the current page or even selecting other pages, $page and $pages does the same thing.Under what different circumstances would you use wire("page") vs $page? Or are they interchangeable?
apeisa Posted June 8, 2013 Posted June 8, 2013 They are the same. Difference is that wire('page') works everywhere, but $page works only on template context (which is of course the most common). So if you are using functions or building modules, you need to use wire('page').
kongondo Posted June 8, 2013 Posted June 8, 2013 As a btw, the alternative syntax to wire('pages') is $wire->pages http://processwire.com/api/include/
Godfrey Posted June 8, 2013 Author Posted June 8, 2013 As a btw, the alternative syntax to wire('pages') is $wire->pages http://processwire.com/api/include/ Oh yeah, I saw that and that's what prompted me to think they may be interchangeable They are the same. Difference is that wire('page') works everywhere, but $page works only on template context (which is of course the most common). So if you are using functions or building modules, you need to use wire('page'). So is it that $page will only work if I make that .php into a template? And wire('') should be used if, for example, I am creating a an includes file containing functions? However that still doesn't really make sense to me because even if I use an include, it will still be rendered into the .php which is attached to a template...
apeisa Posted June 8, 2013 Posted June 8, 2013 This is all more PHP than ProcessWire. All that ProcessWire does is that template files have some predefined variables for nicer api. $page and $pages are among those. Includes are different beast from functions (and from modules). If you include a file, it has all the same variables than the file that included it. So if you include a file from your template file, the included file will have $page and $pages available. Functions have scope. So when you have this code in your template: <?php renderTitle(); function renderTitle() { echo "<h1>$page->title</h1>"; } It will not work, since renderTitle() function doesn't know what $page is. Here is working version of the same template file: <?php renderTitle(); function renderTitle() { $page = wire('page'); echo "<h1>$page->title</h1>"; } or always possible to pass variables: <?php renderTitle($page); function renderTitle($page) { echo "<h1>$page->title</h1>"; } 6
kongondo Posted June 8, 2013 Posted June 8, 2013 (edited) So is it that $page will only work if I make that .php into a template? And wire('') should be used if, for example, I am creating a an includes file containing functions? However that still doesn't really make sense to me because even if I use an include, it will still be rendered into the .php which is attached to a template... $page or $pages work fine in Template Files as long as the they are outside functions. Your Template File can contain functions. Use wire() method in functions (or in your own class). Hence, both wire() and $page/$pages can appear in the same Template File if you so wish. EDIT: Apeisa was faster Edited June 8, 2013 by kongondo 1
Godfrey Posted June 9, 2013 Author Posted June 9, 2013 Ahhh, I see. Of course, scope! Makes sense. Thanks Apeisa, Soma and Kongondo!
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