[SOLVED] Cannot find template with readfile

Hi! For the life of me I cannot seem to get readFile (or the shortcode) working. No matter what I do I just get the following sort of error:

ERROR 2017/12/06 08:13:40 Unable to locate template for shortcode "readfile" in page "parameters/_index.md"

As a side note, I wish it included in the error the absolute path to the file that it’s having an issue with to help with debugging.

Is the path relative to where the shortcode is called? Is it relative to the project? Does it matter if it has a leading slash? I’ve tried reading files all over the project, and I cannot seem to get it to work.

My ultimate goal is to have portions of content that can be easily pulled into various places. I thought a shortcode might be a good fit for this. (I’m confused whether to use whether to use shortcodes or functions in shortcode .html files. I tried both.) I was thinking I do do something like this:

{{< parameter my-parameter >}}

The shortcode would take the first argument and include the my-parameter.md file from the /parameters directory.

When that didn’t work I just tried using the readfile shortcode from the template, but to no avail:

{{< readfile file="comment-block.md" markdown="true" >}}

Can someone please point me in the right direction? I feel like I’m fundamentally misunderstanding something. :man_shrugging: Thanks!

Relative to your content folder.

Thanks! Not sure what I’m doing wrong, then. Here’s the project structure (stripped down to relevant files):

/content
  /parameters
    - _index.md
  /readfiles
    - testing.txt

And here’s the content of _index.md:

---
title: "Parameters"
chapter: true
---
## Description

{{< readfile file="readfiles/testing.txt" >}}

I tried with and without a leading slash. Same error. :confused:

Relative to your parameters/_index.md file, so that would be ../readfiles/testing.txt.

Tried that. No dice. :frowning:

I’m really at a loss, here. Is there a way to see the absolute path of the file it’s not finding?

After some digging, it seems like the error is actually telling me there is no readfile shortcode. Isn’t that inlcuded in Hugo? If it’s new is there something I need to do to and existing project to make it available?

The final answer is “no”. Those shortcodes are just part of the Hugo docs. So to use it I’d need to add the shortcode myself. Ok.

Hugo is somehow simultaneously very intuitive and confusing. :smile:

Hello there,

I am in the same boat. So where do we find the sourcecode for the shortcode? Is there a collection of them I can download somewhere? I’d rather not have to code them myself if I can help it!

EDIT: Here is a link to the code here https://gohugo.io/templates/files/#readfile-example-add-a-project-file-to-content

Thank you kindly for any thoughts

I am utterly dismayed this was this hard. FWIW, Jekyll has built in include of content. Anyhow, here is the source of my /layouts/shortcodes/readfile.html

{{ $toRead := .Get "file" }}
{{ readFile $toRead | markdownify }}

And I invoked it like so:

{{< readfile file="/samples/simple/azure-kubernetes-service/includes/create-aks-cluster-01-body.md" >}}