Jump to content

How to learn syntax, specifically $page


Recommended Posts

Hello everyone,

I'm testing out PW and I think it is fantastic because you can create any type of field in one's pages. I have read most of the documents in the "Docs" menu link. It helps a lot but I really have rudimentary PHP/html knowledge and so everything is confusing. I have very basic questions that I haven't been able to solve. Can someone help?

1. What is the proper syntax for an if-then statement.

<?php if ($page->title="home")
echo '<img src="http://website.com/logo.jpg" width="350" height="190" alt="site title here" class="custom_logo">';
echo '<img src="http://website.com/logo_c.jpg" width="350" height="190" alt="site title here" class="custom_logo">';
This code doesn't throw an error, but it doesn't work. If the page title is "home", then give me this logo, but if it's not, then give me another logo. I'm also very confused by the single quotes/double quotes. If I use double quotes for the echo "....."; portion, it gives an error. When do you use single vs double quotes?
2. I have set up a header.inc that is prepended before the pagebody. This approach works fine (I added header.inc in config.php). I created a new page template named image-page and added the following code:
if($page->image) echo "<img src='{$page->image->first()->url}'>"; 
echo $page->title;
foreach($page->image_details as $name => $value) {
  $label = $page->image_details->label($name);
  echo "<h4>$label</h4>" . $value;
The problem is in the red portion of the code above. I used the same code to echo the title in the header.inc file (<title><?php echo $page->title ?></title>) and the title shows correctly (it shows the correct title - "Shelter" - for example). But the same echo statement in the image-age template (the red portion above), gives me "home". What is causing that behavior?
My website hierarchy is
------Project 1
---------- Shelter
So the title "home" only shows up in the top page. So why is it printing "home" when it should be printing "Shelter"?
3. I'm getting confused as to what I have to learn in terms of syntax when creating a PW website. I know there is no "template" language in PW, but where do I actually learn the syntax? For example, in this code, what part is PHP and what part is PW-specific?
foreach($page->image_details as $name => $value) {
  $label = $page->image_details->label($name);
  echo "<h4>$label</h4>" . $value;

Thank you in advance for any help/insights anyone can provide.



Link to comment
Share on other sites

Hi Jonah, welcome to the PW community!

Your issues mostly seem to pertain to PHP, not ProcessWire specifically. To answer #3, you are correct in that there is no special ProcessWire syntax. In the code you show, there is nothing really “PW-specific”. What ProcessWire does here is provide you with variables you can use in your template, namely $page to access data related to the requested page. The PW-specific knowledge you need is not of syntax, but of those variables, their classes and their methods. In the example, you need to know what kind of object the field image_details is, so you can know to access it with foreach.

In #1, be aware of PHP’s assignment and equality operators! In your if condition, you want to compare values, so you must use == or ===, but never =! You should also wrap the conditional blocks in {curly brackets}. It is not necessary if you only need one statement, but good practice if you ever need to add some more.

Since it is a PHP question, I’ll let StackOverflow explain the difference between 'single' and "double" quotes: http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php :)

Basically, it doesn’t really matter, but double quotes give you more functionality.

I assume #2 is related to #1 in that you have overwritten $page->title with the string "home", because you used the assignment operator = instead of comparing with ==.

  • 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

  • Recently Browsing   0 members

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