Jump to:

3439 Posts in 1028 Topics by 869 members

Template Questions

SilverStripe Forums » Template Questions » How to create a link to an id

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

Page: 1 2
Go to End
Author Topic: 4621 Views
  • Hammy
    Avatar
    Community Member
    49 Posts

    How to create a link to an id Link to this post

    I'm trying to create a link in my template to link to a particular page in the site tree.

    I've tried using LinkToID to link a page in the sitetree that has the id of 10 but have been unable to figure how this or if this is the correct method:

    <a href="$LinkToID.10">Link</a>

    How can i add a link in a template that links to a page in site tree? Is $LinkToID the correct method?

  • NickJacobs
    Avatar
    Community Member
    144 Posts

    Re: How to create a link to an id Link to this post

    Hi Hammy, this may not be the best or most elegant way to do it, but after hacking at it for a while, this worked for me
    I have a FeaturedPage pagetype which then links back to a product category (or any other page) on the site:

    class FeaturedPage extends Page {

    static $has_one = array(
    'FeatureImage' => 'Image',
    'ProductCatLink' => 'Page'
    );

    function getCMSFields() {
    $fields = parent::getCMSFields();
    $fields->addFieldToTab('Root.Content.Features', new TreeDropdownField("ProductCatLinkID", "Select a page to link to", "SiteTree"));
       $fields->addFieldToTab("Root.Content.Features", new ImageField('FeatureImage',"Feature Image"));   
           
    return $fields;
    }
    }

    class FeaturedPage_Controller extends Page_Controller {

    }

    then in the template I do:

    <a class="redir" href="<% control ProductCatLink %> $URLSegment <% end_control %>">More info</a>

    Hope that helps in some way

  • beeonline
    Avatar
    Community Member
    6 Posts

    Re: How to create a link to an id Link to this post

    Hi there,

    I don't know if there's a better way of doing it, but I put a function in my Page.php..

    function GetLinkFromID($id = 1) {
    $do = DataObject::get_one('Page', '`SiteTree_Live`.ID = '.$id, true);
    return ($do->URLSegment);
    }


    This will return the link to any page (or class extending page).
    In your template you'd have something along the lines of

    <a href="$GetLinkFromID(12)">

    where 12 is obviously the id of the page you're wanting to link to.

    If you're within a <% control %> loop, you may need to use $Top.GetLinkFromID(12)

    Hope that helps.

    If you found a better way of doing it I'd be interested to hear.

  • biapar
    Avatar
    Forum Moderator
    435 Posts

    Re: How to create a link to an id Link to this post

    @NickJacobs: But this link only to ProductCatLink page...

  • beeonline
    Avatar
    Community Member
    6 Posts

    Re: How to create a link to an id Link to this post

    It will link to whatever page relates to the id you use in the function..
    e.g. using $GetLinkFromID(12) will return the link to whatever has an id of 12.
    Using $GetLinkFromID(13) will return the link to whatever has an id of 13, etc..

  • zim
    Avatar
    Community Member
    135 Posts

    Re: How to create a link to an id Link to this post

    AAARRGGHHH!!!

    I am trying to use this and all works fine until I try to put this in

    <a href="$GetLinkFromID($StoryUrl)" >

    ... is there a problem dropping $StoryUrl in intsead of nmiuber. It works when i put a number in... and $StoryUrl works by outputting number when not in the stated string?????

    Anyone know why??

  • banal
    Avatar
    Community Member
    901 Posts

    Re: How to create a link to an id Link to this post

    The template parser doesn't resolve variable arguments. That's not going to work.
    But why creating such awkward constructs? Apparently you got the $StoryUrl available, so why not write a method on your controller like this:

    public function StoryLink(){
    return GetLinkFromID($this->StoryUrl);
    }

    Then in the template simply use:

    $StoryLink

  • banal
    Avatar
    Community Member
    901 Posts

    Re: How to create a link to an id Link to this post

    Regarding the method provided by beeonline. I suggest you rewrite your method to something like this:

    function GetLinkFromID($id = 1) {
       $do = DataObject::get_by_id('SiteTree', $id);
       if($do) return $do->Link();
       return '';
    }

    The DataObject::get_by_id method already allows you to get a DataObject by ID. Then use the Link method instead of the URLSegment. It will provide a correct link and will most likely also work with nested URLs. And I also introduced a check if you really get a DataObject.. just in case you pass an invalid ID to the function...

    4621 Views
Page: 1 2
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.