Page 1 of 1

Value of $node

Posted: Thu May 27, 2010 9:48 am
by pjdevries
I'm not quite sure if this is the right forum to post this issue. If not, don't hesitate to say so and/or move this topic elsewhere.

I'm new to CMSMS but have solid experience with Joomla! and some experience with Drupal. A few days ago I purchased the excellent book CMS Made Simple 1.6: Beginner’s Guide and worked my trhough it almost completely, using CMSMS version 1.7.1. Although I'm still struggling a bit with various concepts, there's only one thing I that got me stuck.

In Chapter 10 of the book Sofia (the author) explains how to use Extra Page Attributes. The example she uses, is hiding news on pages by entering the text "nonews" in the Extra Page Attribute 1 field of a page and adding the following logic to the main template:

{if $node->extra1 != "nonews"}
 {news ...}
{/if}

Unfortunately I can not get this to work. I have a two level structure with 5 top level pages: Our Company, Products, Client Center, Contact Us and Sitemap. Only two of them, Our Company and Client Center, have child pages. I entered the "nonews" value in the Extra Page Attribute 1 field of the Contact Us page, but unfortunately the news still shows up. So I entered some debug information in the template to inspect various variables and this is what I see:
  • {get_template_vars} displays the expected page_name and page_alias (contact-us).
  • {page_attr_key="extra1"} displays "nonews" as expected.
  • {$node|print_r} displays information about the wrong $node, namely about page Sitemap.
It's obvious why the logic doesn't work: I'm inspecting the wrong node. In fact, no matter which page I navigate to, I never see the node data of the page I actually navigated to. So the question is: how do I get hold of the correct node object? Can someone point me in the right direction?

Re: Value of $node

Posted: Fri May 28, 2010 6:56 am
by pjdevries
Interesting suggestion. Not sure if she is eager to answer questions from all of her readers though. I hoped the CMSMS forum was the appropriate place for this kind of questions.

Your short answer more or less implies that either the availability and/or actual value of $node are specific for the situation descried in the book. To make things less book and more general CMSMS related, let me rephrase my question a bit:

1. Are $node and $nodelist generally available on each and every page, or are they context/module specific?
2. If they are, how do I obtain the current $node?

Re: Value of $node

Posted: Fri May 28, 2010 10:46 am
by Peciura
Variables {$nodelist} and {$node}  are primarily used in menu templates. As long your page contains menu, these variables will be available in page template. {$nodelist} is iterated by {$node} so generally {$node} will be the last menu item and is not much useful in page.

Code: Select all

{if $node->extra1 != "nonews"}...
I would say this code bit is useful only while looping {$nodelist} in menu template.

To find out all stuff about current page inspect {$content_obj}. Here you will find more debugging options http://forum.cmsmadesimple.org/index.ph ... #msg204523 . I prefer UDT {var_dump} because it shows variable type and string length.

Also you might find useful module CGSimpleSmarty and tag (plugin) {content_dump}.

Re: Value of $node

Posted: Fri May 28, 2010 11:53 am
by pjdevries
Thank you for the extensive and valuable feedback.

I took a close look at the menu template and see what you mean. Because the menu template already contains a check for $node->current, I used that to initialize a variable $currentNode, which I can then use in my main template instead of the $node I used before.

I will definitly check out the pages and tags you mentioned.

Re: Value of $node

Posted: Sat May 29, 2010 3:36 pm
by NaN
If you only want to check the extra attribute of the current page you don't need to access the $nodelist.
You can directly access any content property of the current page by using the core plugin {page_attr}:

Code: Select all


{page_attr key="extra1" assign="news"}

{if $news != 'nonews'}
   {news ... }
{/if}

Or just using the template var {$content_obj}.
{$content_obj} is an object that contains all content related info of the current page.
You can also access all content props this way:

Code: Select all


{$content_obj->mProperties->mPropertyValues.[ENTER_PROPNAME_HERE]}

so in your template you can check the extra1 field for "nonews" this way:

Code: Select all


{if $content_obj->mProperties->mPropertyValues.extra1 != 'nonews'}
   {news ... }
{/if}


To trigger modules you can also do it the way it is done in the Theme "GalaPurity".
There additional content blocks are used in the page template.
That means in the template there is - besides the general {content} - something like this:

Code: Select all


{content block="news" oneline=true label="Show News?" assign="show_news"}

When editing a page you now will have an additional field labeled "Show news" that is just a simple textinput.
There you can enter e.g. "yes"/"no" or just use numbers "1" (to show it) and "0" (to hide it).
In your page template you now can check this content block for its value:

Code: Select all


{if $show_news=="1"}
   {news ... }
{/if}


And last not least there is module called AdvancedContent i recently created that allows you to add several controls to the backend when editing a page. So you could add a dropdown to your edit area using this in your template:

Code: Select all


{capture assign="show_news"}{AdvancedContent block="news" label="Show News?" type="dropdown" items="yes|no" values="1|0"}{/capture}
{if $show_news}
   {news ... }
{/if}


Cheers and have fun with CMSms ;)

NaN

Re: Value of $node

Posted: Wed Jun 23, 2010 6:52 pm
by luminous
I too am going to express my gratitude for the time taken by people explaining this.  I too have had the same issue, and was slowly tearing what hair I have left out of my head before I found this.

Thanks for explaining this  ;D

I do feel that this whole web design thing is a bit of a vertical learning curve atm :(

Re: Value of $node

Posted: Thu Jun 24, 2010 7:27 am
by pjdevries
I like to thank NaN for the contribution as well. Sorry for the late reply, but sometimes I don't get notifications of new forum posts, so I completely missed it until now.