Jump to:

3393 Posts in 976 Topics by 852 members

Template Questions

SilverStripe Forums » Template Questions » Control a page by ID rather than URL?

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

Page: 1
Go to End
Author Topic: 2390 Views
  • neilcreagh
    Avatar
    Community Member
    88 Posts

    Control a page by ID rather than URL? Link to this post

    In my templates, how would I add specific page control by ID rather than URL? eg.

    <% control Page(PageID) %>

    <% control ChildrenOf(PageID) %>

    On a couple of occasions a client will inadvertently break these links by changing the page title (and/or the meta title) - so controlling by URL is not always ideal.

    Any ideas?

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Control a page by ID rather than URL? Link to this post

    You could write a function that does that for you quite easily.
    Place this in your Page class:

    function PageById($id){
       return DataObject::get_by_id("SiteTree", (int)$id);
    }

    Then in your template you can access pages by id like this:

    <% control PageById(1) %>$Title<% end_control %>

  • web2works
    Avatar
    Community Member
    50 Posts

    Re: Control a page by ID rather than URL? Link to this post

    thank you this has helped me out a lot.

  • NickJacobs
    Avatar
    Community Member
    144 Posts

    Re: Control a page by ID rather than URL? Link to this post

    You can also just simply use the ID in place of the URLSegment,ie:

    <% control childrenOf(20) %>

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