Not finding Relative path CSV

I’m trying to export a CSV to table and can’t find a way to find the file, so I went crazy mode:

ERROR The “csv-to-table” shortcode was unable to find test.csv. See “C:\Users\me\Desktop\pixdocs\content\docs\siglas_index.md:12:1”

_index.md:12:1

### Title
{{< csv-to-table "test.csv" >}}

csv-to-table.html

{{ with $file := .Get 0 }}
  {{ with resources.Get $file }}
    {{ with . | transform.Unmarshal }}
      <table>
        <thead>
          <tr>
            {{ range index . 0 }}
              <th>{{ . }}</th>
            {{ end }}
          </tr>
        </thead>
        <tbody>
          {{ range after 1 . }}
            <tr>
              {{ range . }}
                <td>{{ . }}</td>
              {{ end }}
            </tr>
          {{ end }}
        </tbody>
      </table>
    {{ end }}
  {{ else }}
    {{ errorf "The %q shortcode was unable to find %s. See %s" $.Name $file $.Position }}
  {{ end }}
{{ else }}
  {{ errorf "The %q shortcode requires one positional argument, the path to the CSV file relative to the assets directory. See %s" .Name .Position }}
{{ end }}

hugo env
hugo v0.128.2-de36c1a95d28595d8243fd8b891665b069ed0850 windows/amd64 BuildDate=2024-07-04T08:13:25Z VendorInfo=gohugoio
GOOS=“windows”
GOARCH=“amd64”
GOVERSION=“go1.22.2”

resources.Get operates on global resources, ie those in and below assets. You’re looking for .Resources.Get.

Why on earth one would want to have two methods working on different structures differ by upper/lower case spelling is beyond me.

1 Like

We’ve had this conversation before. One is a function, while the other is a method.

That doesn’t make it any easier to understand or remember. But it will probably not change anyway.

When I use the .Resources.Get
csv-to-table.html:3:20": execute of template failed: template: shortcodes/csv-to-table.html:3:20: executing "shortcodes/csv-to-table.html" at <.Resources.Get>: can't evaluate field Resources in type string

using resources.Get

_index.md:
{{< csv-to-table "excel/test.csv" >}}
the correct path to drop the file would be: static/assets/csv/test.csv ?

, couldn’t find it anyway :frowning:

Dot is a string here (as the message rightly says). That’s the consequence of using with $file := .Get 0 Inside the with, dot is whatever .Get 0 returned.
You could use a simple assignment instead ($file := .Get 0). Or use with $.Page.resources.Get $file.

And read about context and the dot in Hugo.

1 Like

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