Jump to content

Basic beginner question - setting defaults in init.php


SamC
 Share

Recommended Posts

Hi,

New to PW and more a designer than a developer, currently learning Javascript with basic knowledge of PHP (on the never-ending todo list) and beginner level experience in other languages, C++, Python, Java. I have used Umbraco, Drupal 7 (mainly), Wordpress, Craft CMS (my favourite for past year or so) before this and CMS is something I love playing around with! I am looking for something I can get more permanently involved with and I had a good look over the code for the 'Default Site Profile (Intermediate Edition)' PW site.

As far as my PHP knowledge goes, I can decipher renderNav(), but I'm struggling with renderNavTree().

One thing I don't get initially is this in '_init.php':

$title = $page->get('headline|title'); // headline if available, otherwise title
$content = $page->body;
$sidebar = $page->sidebar;

I understand that these variables are for usage elsewhere as defaults. However, why does the $title variable need '$page->get' when the other ones just need '$page->body' and '$page->sidebar'?

In '/setup/fields' I can see that 'title' and 'body' are both fields so not sure why these are different. Any advice is most appreciated.

Thanks.

Edited by SamC
More descriptive title
Link to comment
Share on other sites

Hi SamC and welcome.

20 minutes ago, SamC said:

$page->get('headline|title');

is used to get either headline or title, depending whether both or only title are populated. If headline has a value it is returned, or else title.

In PW, $page->field returns one value, which may be null.

$page->get('selector') returns one value from multiple possible values (like 'headline|title' above).

$page->find('selector') will return zero or more values in an array.

Typical cases where headline and title may be different would be a short entry in navigation (eg Home) and a longer heading for the page (eg Welcome to Our Site).

  • Like 3
Link to comment
Share on other sites

Hi DaveP,

Thanks, that was most useful. So would you do stuff like:

if ($page->field) {
	echo 'field value here'; //or whatever
}

What I'm asking is if a page field can either (1) not exist, (2) exist and be empty or (3) exist and contain data, to get the value or print nothing to screen (without errors), will points (1) and (2) both return NULL? Or do you first have to check a field actually exists? Thanks.

Link to comment
Share on other sites

1 hour ago, SamC said:

What I'm asking is if a page field can either (1) not exist, (2) exist and be empty or (3) exist and contain data, to get the value or print nothing to screen (without errors), will points (1) and (2) both return NULL? Or do you first have to check a field actually exists?

 Ryan has some good advice about field checks in this blog post.

  • Like 4
Link to comment
Share on other sites

29 minutes ago, horst said:

But don't forget to play with code too. :)

... and welcome to the forums, @SamC

Yeah for sure! Been creating a simple blog page with a couple of new templates (blog-index.php and blog-entry.php). Wanted to show submitted by date and name (other than my username). Thanks to this forum (how to add fields to system templates) and a youtube video (general templating and curly braces usage within a string, handy!), managed to cobble together:

//blog-entry.php
$publish_date = date('d-M-y', $page->created);
//custom field displayname in user template
$created_by = $page->createdUser->displayname;

$content = "Published on: {$publish_date} by {$created_by}";
//show above post entry body field
$content .= $page->body;

Seems quite straightforward so far (famous last words), feels like Craft, but without Twig. I've got a fair bit of free time at the moment which doesn't happen all that often so a great opportunity to learn :-)

Thanks for making me feel so welcome everyone.

  • Like 2
Link to comment
Share on other sites

@SamC If you need a template engine, there are maintained modules to build upon:

Smarty, Twig, Jade and more:

http://modules.processwire.com/modules/template-engine-factory/

Latte only:

http://modules.processwire.com/modules/template-latte-replace/

Recommended tutorial to jump start: Basic ProcessWire website workflow

http://blog.mauriziobonani.com/tags/#processwire

  • Like 1
Link to comment
Share on other sites

1 hour ago, szabesz said:

@SamC If you need a template engine, there are maintained modules to build upon:

Smarty, Twig, Jade and more:

http://modules.processwire.com/modules/template-engine-factory/

Latte only:

http://modules.processwire.com/modules/template-latte-replace/

Recommended tutorial to jump start: Basic ProcessWire website workflow

http://blog.mauriziobonani.com/tags/#processwire

That's some awesome info, thanks! Will probably try and get Twig up and running at some point, I find PHP mashed in with HTML all over the place quite difficult to comprehend sometimes.

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