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.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Template Questions /

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

Dynamically get URLSegment by Page ID

Go to End

3 Posts   1785 Views


Community Member, 7 Posts

12 October 2013 at 5:57am

I'm new to SS and having a difficult time finding answers to my questions for SS 3.0+, so I decided to giving posting to the forum a shot :-)

The design I'm coding is a one page site with multiple vertical and horizontal "panels". Some of the panels have buttons that link to other panels with a #. I've set it up so the panels are subpages and the panel IDs are the URLSegment for each page. In order to add buttons to each section, I set up a gridfield in the CMS and I am using the Link Field module to allow the user to input either an internal, external, or email link. When the user selects an Internal Page, the module returns the ID for that page. I need to be able to take that ID and grab the URLSegment for the page it's referencing so I can create an anchor link to that section in the page.

Here's the code for the buttons from my template:

<div class="row">
<% if $Link.Type == "Internal" %>
<a href="#{$Page($Link.Internal).URLSegment}" class="fake_button js-hide-cursor">$Text</a>
<% else_if $Link.Type == "External" %>
<a href="{$Link.External}" class="fake_button js-hide-cursor" target="_blank">$Text</a>
<% else_if $Link.Type == "Email" %>
<a href="mailto:{$Link.Email}" class="fake_button js-hide-cursor">$Text</a>
<% else %>
<button class="fake_button js-hide-cursor">$Text</button>
<% end_if %>

Basically, I need something like {$Page($Link.Internal).URLSegment} where I can pass in the PageID from $Link.Internal and retrieve the URLSegment for that page.



Forum Moderator, 5523 Posts

12 October 2013 at 5:28pm

Pretty sure the link field module ( gives you the ability to get the whole Page object.

<% if $Link.Page %>
<a href="#$Link.Page.URLSegment" class="fake_button js-hide-cursor">$Text</a>
<% end_if %>

Note that URLSegment will not be unique (i.e two pages 'about/silverstripe' and 'products/silverstripe' will both have the URLSegment 'silverstripe'. The link method will generate the full link to the page (but that won't be ID friendly :))


Community Member, 7 Posts

15 October 2013 at 6:18am

Thanks for responding so quickly, Willr!

I was using a different link field module (, but I tried the one you linked to and it worked for my purposes. Thanks!

Also, these are currently just for top level pages, so the URLSegment should be unique for those, correct?