Peter Harley Broom
Peter Harley Broom

Reputation: 115

Silverstripe hash link rewriting putting unwanted slash in link

Ok so I am developing a site in Silverstripe CMS that is using anchor links on one of it's pages. Thing is that in Silverstripe there is a rewrite going on that puts slashes before the hash in your links.

So in the docs it explains that you can put this in your YAML to disable slashes before hashes: http://doc.silverstripe.org/en/developer_guides/templates/how_tos/disable_anchor_links/

Which I have done like so (YAML validates ok):

_config/app.yml:

SSViewer: rewrite_hash_links: false

And then in my template file this is how I am constructing my link with anchor:

<a href="$ParentPage.Link#$URLSegment">Link</a>

(note that this template file is for a dataobject, I'm not sure if that has any baring)

And the outputted link is:

/cnc-machining/#made-to-order

but should be:

/cnc-machining#made-to-order

I'm all out of ideas. Any pointers?

Upvotes: 0

Views: 1087

Answers (1)

wmk
wmk

Reputation: 4626

In your DataObject's getLink() method you can simply remove the trailing slash using rtrim:

public function getLink() {
    //remove trailing slash from parent link
    $parentLink = rtrim($this->ParentPage()->Link(), '/'); 
    return $parentLink . '#' . $this->URLSegment;
}

Now in your template just run in DataObject's scope:

<a href="$Link">Link</a>

Though i didn't notice any disadvantage with having the trailing slash in the url.

HTH, wmk

Upvotes: 2

Related Questions