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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

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

Dynamically get URLSegment by Page ID


Go to End


3 Posts   2572 Views

Avatar
Josiah

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 %>
</div>

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.

Thanks!

Avatar
Willr

Forum Moderator, 5523 Posts

12 October 2013 at 5:28pm

Pretty sure the link field module (https://github.com/lrc/silverstripe-link-field) 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 :))

Avatar
Josiah

Community Member, 7 Posts

15 October 2013 at 6:18am

Thanks for responding so quickly, Willr!

I was using a different link field module (https://github.com/webtorque7/link-field), 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?