How to get `.File.UniqueID` from a shortcode?

Hi there,

I’m using a partial to insert Cusdis comments in templates.

Here is the code, which works fine as a partial:

{{ if .Site.Params.CommentsCusdisID }}

    <h2>Discussion:</h2>
    <div id="cusdis_thread" data-host="https://cusdis.com"
      data-app-id="{{ .Site.Params.CommentsCusdisID }}"
      data-page-id="{{ .File.UniqueID }}"
      data-page-url="{{ .Permalink }}"
      data-page-title="{{ .Title }}">
    </div>
    <script defer src="https://cusdis.com/js/widget/lang/fr.js"></script>
    <script async defer src="https://cusdis.com/js/cusdis.es.js"></script>
{{end}}

I would like to turn this into a shortocode, to insert the form exactly where I want to.

Unfortunately, moving the very same code to layouts/shortcodes/cusdis.html gives the following error:

failed to process shortcode: [...]
execute of template failed at <.File.UniqueID>: can’t evaluate field File in type *hugolib.ShortcodeWithPage

Any hint?

.File is a method on a Page object:
https://gohugo.io/methods/page/file/

In a shortcode, you need to use .Page.File.something:
https://gohugo.io/methods/shortcode/

1 Like

Thanks a lot!

Here is the final code:

{{- /* Comments with cusdis.com */ -}}

{{ if .Site.Params.CommentsCusdisID }}

    <div id="cusdis_thread" data-host="https://cusdis.com"
      data-app-id="{{ .Site.Params.CommentsCusdisID }}"
      data-page-id="{{ .Page.File.UniqueID }}"
      data-page-url="{{ .Page.Permalink }}"
      data-page-title="{{ .Page.Title }}">
    </div>
    <script defer src="https://cusdis.com/js/widget/lang/fr.js"></script>
    <script async defer src="https://cusdis.com/js/cusdis.es.js"></script>
{{end}}

{{- /* Comments area end */ -}}

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.