Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Template Questions /

<% if InSection(page-ID) %> instead of <% if InSection(page-url) %>?


Reply


7 Posts   2186 Views

Avatar
janulka

Community Member, 80 Posts

4 February 2011 at 3:27am

Is it possible to have certain content in template based on page ID instead of page-url?

It is very easy for a client to change page url accidentally (when changing page title), then the <% if InSection(page-url) %> in template obviously does not work anymore..

thanks :)

Avatar
swaiba

Forum Moderator, 1804 Posts

4 February 2011 at 4:47am

Edited: 04/02/2011 4:51am

I would say yes, but it would read much better if you did something like...

<% if InSection(PageName) %>

then in controller...

function InSection($strPageName) {
if ($strPageName=='mypagename') {
//find page by any method you can, e.g. DO:get_by_id('SiteTree',ID);
}
}

it would just look better than...

<% if InSection(23462) %>


:)

Avatar
janulka

Community Member, 80 Posts

4 February 2011 at 5:05am

I actually do want to use ID since this is not possible to edit from within the CMS.. Client can easily change page name, url, and I need something that they cannot change..
I can very little php, can someone please help me with function to copy/paste?
thank you :)

Avatar
swaiba

Forum Moderator, 1804 Posts

4 February 2011 at 5:22am

Edited: 04/02/2011 5:24am

er... I already did :)
I just didn't want your templates full of id's!, I thought it would be more readable to use a label and then hide the ugly hardcoding of ips somewhere else,

function InSection($strPageName) {
if ($strPageName=='mypagename') {
//find page
$doSiteTree = DataObject:get_by_id('SiteTree',HARDCODEYOURIDHERE);
}
}

I think the confusion is with "PageName" that is just a label string you would use to identify in the template to make it readable and then within your function in the controller you actually have the id. I do think you need something else here as hardcoding lookups by id are not a great idea.

If all of these Pages are of type "Page" you are going to have to reply on some of the data that the user could change. If each page is a different type and they are allowed only one type of page (although again if it's a user they will probably create two pages of that type :) I love users - I'm one myself).

You could always add a literal field, that places a label above the page url saying "CHANGE THIS AND DIE!!!! xx hugs janulka" :)

Avatar
janulka

Community Member, 80 Posts

4 February 2011 at 5:38am

ye, the warning would probably do the work :)

but back to your code.. this way I will have to have function for each ID I use, is there a way to have only one function and then use this function with ID in template - which would work similar as <% if InSection(page-url) %> but work with ID instead.. and ideally, of course, this would work for the page and children of that page :)

Avatar
swaiba

Forum Moderator, 1804 Posts

4 February 2011 at 5:48am

Edited: 04/02/2011 6:01am

ahhhh

I'm sorry I forgot that that is an actual ss function !

hmmm since I'v not used this I'm not entirely sure, you could try this in your page_controller...

function InSectionByID($pageID) {
$doSiteTree = DataObject:get_by_id('SiteTree',$pageID);
return $this->InSection($doSiteTree->URLSegment);
}


but that is still going to rely on the page that is looked up's url segment...

you could just rewite the function to do as you'd like, name it something else and use that...

   /**
    * Check if this page is in the given current section.
    *
    * @param string $sectionName Name of the section to check.
    * @return boolean True if we are in the given section.
    */
   public function InSection($sectionName) {
      $page = Director::get_current_page();
      while($page) {
         if($sectionName == $page->URLSegment)
            return true;
         $page = $page->Parent;
      }
      return false;
   }

Avatar
janulka

Community Member, 80 Posts

5 February 2011 at 9:33am

of course, I can just modify the InSection function..

this is what should work fine?

public function InSectionID($sectionNameID) {
      $page = Director::get_current_page();
      while($page) {
         if($sectionNameID == $page->ID)
            return true;
         $page = $page->Parent;
      }
      return false;
   }

thank you :)