Jump to content

wire("page[s]") vs $page[s]?


Godfrey
 Share

Recommended Posts

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?

 

Link to comment
Share on other sites

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').

Link to comment
Share on other sites

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... 

Link to comment
Share on other sites

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>";
}
  • Like 6
Link to comment
Share on other sites

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 by kongondo
  • Like 1
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...