}} but I get an error parse failed unexpected" /> }} but I get an error parse failed unexpected" /> }} but I get an error parse failed unexpected"/>

Hugo: how to create a link to page in a shortcode?

1.6k Views Asked by At

In a shortcode I want to create a link to an hugo page, how can I do this ?

I tried

{{< ref "page_name" >}}

but I get an error parse failed unexpected "<" in command .

I could use for example <a href="https:/www.example.com/test/">Test</a> but this is tied to the domain where the site is hosted, I want to be able to use it without this constraint.

Thanks for the help

3

There are 3 best solutions below

4
res1 On BEST ANSWER

I found how I can do this, this link was helpful:

https://github.com/parsiya/Hugo-Shortcodes/blob/master/shortcodes/xref.html

so in shortcode I used:

{{ $path := .Get "path" }}
{{ $path := trim $path "/" }}
<!-- now pass it to the relref function -->
{{ $relreflink := relref . $path }}

<a href="{{ $relreflink }}" title="Some title" rel="nofollow" target="_blank">Some text</a>

In markdown code then I can use:

{{< my-shortcode path="/my_page" >}}

It seems to work.

1
giulp On

Maybe what you need is relref

From the documentation:

{{< relref "document" >}}
{{< relref "document.md" >}}
{{< relref "#anchor" >}}
{{< relref "/blog/my-post.md" >}}
0
Rogelio On

[]() <- Hugo pages are markdown - this is the way to create a link with markdown.

So:

[Test](/test/)

If you want to specifically use a shortcode, the other answer of various shortcodes works - but - the above gives you describe that you want.