Jump to:

3432 Posts in 1025 Topics by 865 members

Template Questions

SilverStripe Forums » Template Questions » Dynamically get URLSegment by Page ID

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

Page: 1
Go to End
Author Topic: 590 Views
  • Josiah
    Community Member
    7 Posts

    Dynamically get URLSegment by Page ID Link to this post

    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.


  • Willr
    Forum Moderator
    5482 Posts

    Re: Dynamically get URLSegment by Page ID Link to this post

    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 )

  • Josiah
    Community Member
    7 Posts

    Re: Dynamically get URLSegment by Page ID Link to this post

    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?

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.