Jump to:

3392 Posts in 976 Topics by 852 members

Template Questions

SilverStripe Forums » Template Questions » <% if InSection(page-ID) %> instead of <% if InSection(page-url) %>?

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1931 Views
  • janulka
    Avatar
    Community Member
    80 Posts

    <% if InSection(page-ID) %> instead of <% if InSection(page-url) %>? Link to this post

    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

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: <% if InSection(page-ID) %> instead of <% if InSection(page-url) %>? Link to this post

    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) %>


  • janulka
    Avatar
    Community Member
    80 Posts

    Re: <% if InSection(page-ID) %> instead of <% if InSection(page-url) %>? Link to this post

    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

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: <% if InSection(page-ID) %> instead of <% if InSection(page-url) %>? Link to this post

    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"

  • janulka
    Avatar
    Community Member
    80 Posts

    Re: <% if InSection(page-ID) %> instead of <% if InSection(page-url) %>? Link to this post

    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

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: <% if InSection(page-ID) %> instead of <% if InSection(page-url) %>? Link to this post

    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;
       }

  • janulka
    Avatar
    Community Member
    80 Posts

    Re: <% if InSection(page-ID) %> instead of <% if InSection(page-url) %>? Link to this post

    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

    1931 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.