silverstripe-framework/docs/en/02_Developer_Guides/01_Templates/How_Tos/03_Disable_Anchor_Links.md
Aled Brown ff1a0dd6fc
renderWith Namespace for Template Loading
Just calling MyCustomTemplate does not work in SS 4.2.X. Adding the full Namespace as a path is now required.
2018-11-21 15:28:12 +00:00

54 lines
1.7 KiB
Markdown

title: Disable Anchor Rewriting
# Disable Anchor Rewriting
Anchor links are links with a "#" in them. A frequent use-case is to use anchor links to point to different sections of
the current page. For example, we might have this in our template:
```ss
<ul>
<li><a href="#section1">Section 1</a></li>
<li><a href="#section2">Section 2</a></li>
</ul>
```
Things get tricky because of we have set our `<base>` tag to point to the root of the site. So, when you click the
first link you will be sent to http://yoursite.com/#section1 instead of http://yoursite.com/my-long-page/#section1
In order to prevent this situation, the SSViewer template renderer will automatically rewrite any anchor link that
doesn't specify a URL before the anchor, prefixing the URL of the current page. For our example above, the following
would be created in the final HTML
```ss
<ul>
<li><a href="my-long-page/#section1">Section 1</a></li>
<li><a href="my-long-page/#section2">Section 2</a></li>
</ul>
```
There are cases where this can be unhelpful. HTML anchors created from Ajax responses are the most common. In these
situations, you can disable anchor link rewriting by setting the `SSViewer.rewrite_hash_links` configuration value to
`false`.
**app/_config/app.yml**
```yml
SilverStripe\View\SSViewer:
rewrite_hash_links: false
```
Or, a better way is to call this just for the rendering phase of this particular file:
```php
use SilverStripe\View\SSViewer;
public function RenderCustomTemplate()
{
SSViewer::setRewriteHashLinks(false);
$html = $this->renderWith('My/Namespace/MyCustomTemplate');
SSViewer::setRewriteHashLinks(true);
return $html;
}
```