Search code examples
hugohugo-shortcode

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


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


Solution

  • 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.