Jump to content

what is best practise for variables?


bwakad
 Share

Recommended Posts

Does anyone have a 'best practise' for defining variables? A PW function / practise? I was reading the _main.inc / _init.inc tutorial.

In that, variables are defined as $fields, and used as : $variable append $field.

Where as my $sorted is not a $field, so I use :  $field append $variable.

In head.inc I output:

<h1><?php echo $page->title;?></h1>
<h6><?php echo $page->get("extrainfo")." ".$sorted; ?></h6>

The $sorted is a variable used only on certain pages. Obviously I get a undefined variable if I am not on a page were this is used. Using:

if($sorted) echo $sorted;

I still receive undefined variable because head.inc is loaded before content. I know php can do this with:

<?php if (isset($sorted)) echo $sorted;?>
Link to comment
Share on other sites

To avoid those if isset etc. you can list all your variables that you use in some templates initially in the _init.inc or what ever you use that is loaded before all others. (At least, you also could do it in the site/config.php, but I wouldn't)

_init.php:

$sorted = '';  // if it is for Output, I use an empty string initially

$variable1 = '';

$variable2 = '';

$variable3 = '';

Link to comment
Share on other sites

All the variables coming from the pw api are not really single variables but properties and methods of an object. Therefore they return NULL if you try to get an non existent property and an php error if you try to call a non existent method. So for the api stuff " if($page->randomProperty) … " is most of the times enough, while for normal variables you have to go with " if(isset($stringVariable)) … ".

If you really only want to check on the existance of an property you need to do the following, because otherwise the if statement would also not hit, if the randomProperty is false.

if($page->randomProperty != NULL) 

$variable3 = '';

Maybe use double quotes? This could lead to errors because of missing quotes, if somebody manually copies it.

Link to comment
Share on other sites

Just for imformation: I have debug ON....

I tried Horst approach, but did not work:

head.inc

<?php 
include("./inc/functions.php");
// after this follows 
<html>
<head> etc
<body> opening

functions.php :

<?php
$sorted = ""; // very first line
//rest of code

content.php :

<?php
include("./inc/head.inc");
// include something else - were variable is used ...
Link to comment
Share on other sites

Maybe use double quotes? This could lead to errors because of missing quotes, if somebody manually copies it.

You mean I should use double-quotes for better readability? You are right! I will try to do so. (but may be hard for me, :) )

  • Like 1
Link to comment
Share on other sites

Horst method is working fine to NOT display undefined variable.

problem is this:

on content (after head.inc) I define the variable value: $sorted = "sort by date joined (new first)";

So this method does not give undefined variable, but also does not display if there is a value!

If I DON'T use the $sorted = ""; it gives undefined variable is there is no value, and does display value if there is one.

Link to comment
Share on other sites

Most people here use a delageted approach to templating which in short looks like this. This will either echo nothing or your default value, if present. More info on this approach can be found here: http://processwire.com/docs/tutorials/how-to-structure-your-template-files/page4

// _init.php

$sort = "";

// template.php
include "_init.php";

$sort = "sort by date …";

include "_main.php";

// _main.php
[…]
echo $sort;
[…]

If you don't use this way of templating, the thoughts behind this is, that you have a php file which is always called before everything else, where you define all your variables. Then you don't get the php error for undefined variables if you just echo them. Everything after this file then overrides the empty string to actually display stuff in places where the variable gets echo'd.

  • Like 1
Link to comment
Share on other sites

I even created the _init.php as per your example but no results if there should be one:

// _init.php

$sort = "";

// browse.php
include "_init.php";

// some code to determine what $sort will be
$sort = "sort by date …";

include "head.php"; // here is were my $sort get outputted ...
include "grid.php";
include "foot.php";

// grid.php
[…]
echo $content;
[…]
Link to comment
Share on other sites

Most people here use a delageted approach to templating which in short looks like this. This will either echo nothing or your default value, if present. More info on this approach can be found here: http://processwire.com/docs/tutorials/how-to-structure-your-template-files/page4

// _init.php

$sort = "";

// template.php
include "_init.php";

$sort = "sort by date …";

include "_main.php";

// _main.php
[…]
echo $sort;
[…]

If you don't use this way of templating, the thoughts behind this is, that you have a php file which is always called before everything else, where you define all your variables. Then you don't get the php error for undefined variables if you just echo them. Everything after this file then overrides the empty string to actually display stuff in places where the variable gets echo'd.

This is also why i started to explain:

In that, variables are defined as $fields, and used as : $variable append $field.

Where as my $sorted is not a $field, and I use :  $field append $variable.

So I can't use - edit  -:

$sort= $page->get("headline|title"); // variable append field value

$sort = $page->body; // variable append field value

because I need:

$page->get("extrainfo").$sort ; ??? (in head.inc)

You're echoing $content while setting $sort, so there' still something missing in your example. 

confusion. lol. $content is just the output of html on grid.php. (without $page->title and $sort value).

---

I guess a field can be accesed from any place by using $page->get ... but a variable is not accesible like that. Maybe I should just re-arrange my markup to output the variable in content, in stead of the head (where the page title is)...

Link to comment
Share on other sites

First of all $page->get("extrainfo").$sort; doesn't make much sense on its own. If you want to dynamicly build the name of the field to query use this:

$page->get("extrainfo".$something);

But I still can't get my head around what you're trying to archieve. A field can't be sorted. Only array's or in pw pagearrays. If extrainfo is a page field or a repeater, you can use this.

$page->extrainfo->sort($sortSelector);

Edit: 

Read your first post again. If you just want to append $sort to the string this should work: $page->get("extrainfo").$sort;

Either you get only the field or with $sort appended, if it is overridden from it's empty init state.

Link to comment
Share on other sites

Maybe this will explain:

browse.php
// in _init.php I have one line: $sort = "";
include ("./inc/_init.php");

// sorting, here $sort get a value
if($input->get->sort) {
   switch($sort) {
      case "asc": do something; $sort = "you sorted results on blah blah blah"; break;
      default: do something; $sort = "sorted by name ascending"; break;
   }
}

$selects = $pages->find($selector);

// then I include the following, and in head.inc I echo $page->title and $sort.

include("./inc/head.inc"); 
include("./inc/layout.inc");
include("./incfoot.inc");

THIS: $page->get("extrainfo").$sort; WILL WORK,

BUT UNDEFINED VARIABLE when I am on other page that does not use browse.php.

Even when I used _init.php


First of all $page->get("extrainfo").$sort; doesn't make much sense on its own. If you want to dynamicly build the name of the field to query use this:

$page->get("extrainfo".$something);

But I still can't get my head around what you're trying to archieve. A field can't be sorted. Only array's or in pw pagearrays. If extrainfo is a page field or a repeater, you can use this.

$page->extrainfo->sort($sortSelector);

Edit: 

Read your first post again. If you just want to append $sort to the string this should work: $page->get("extrainfo").$sort;

Either you get only the field or with $sort appended, if it is overridden from it's empty init state.

I never said I wanted to sort fields ....

Link to comment
Share on other sites

I still didn't grasped what you're trying to archive, because your last code piece was missing, so I tried to help you with what could be what you're after. 

As long as you include the _init.php in each template there shouldn't be any error in echoing the $sort variable, no matter where in your code. There must only ever be the include statement somewhere before the echoing.

  • Like 1
Link to comment
Share on other sites

this was the problem: As long as you include the _init.php in each template...

Thanks for that! I did not think about including it in other templates... dang.... topic was getting so longggggg.

@ martijn - I did not understood your respons. But maybe because I typed $sort and implies API ->sort ...?

I simply used $sort because LostKobrakai used $sort. In my topic first message I used $sorted ...

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