Silverstripe hash link rewriting putting unwanted slash in link

拥有回忆 提交于 2019-12-11 20:17:06

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/28365057/silverstripe-hash-link-rewriting-putting-unwanted-slash-in-link

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!