银线条散列链接重写将链接中不需要的斜杠 [英] Silverstripe hash link rewriting putting unwanted slash in link
问题描述
好的,我正在Silverstripe CMS中开发一个网站,该网站在其中一个网页上使用锚链接。事情是在Silverstripe有一个重写正在把你的链接中的哈希放在斜杠之前。
所以在文档中它解释说你可以把它放在你的YAML中在哈希之前禁用斜线: http://doc.silverstripe.org/en/ developer_guides / templates / how_tos / disable_anchor_links /
我已经这样做了(YAML验证可以):
_config / app.yml:
SSViewer:rewrite_hash_links:false
然后在我的模板文件中,这就是我如何使用anchor构建链接:
< a href =$ ParentPage.Link#$ URLSegment>链接< / a>
(请注意,这个模板文件是用于数据对象的,我不确定它是否有任何baring )
输出的链接是:
$ b $ cnc-machining /#定制 p>
但应该是:
$ b/ cnc-machining#定制
我完全没有想法。任何指针?在您的DataObject的getLink()方法中,您可以简单地使用 //php.net/rtrimrel =nofollow> rtrim :
public function getLink(){
//从父链接中删除尾部斜杠$ b $ parentLink = rtrim($ this-> ParentPage() - > Link(),'/');
返回$ parentLink。 '#'。 $这 - > URLSegment;
}
现在在您的模板中运行DataObject的范围:
< a href =$ Link>连结< / a>
尽管我没有注意到在URL中使用尾部斜线没有任何不利之处。
HTH,wmk
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?
解决方案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
这篇关于银线条散列链接重写将链接中不需要的斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!