Loading a page resources within a page Markdown page resource

Hello,

I am attempting to access a page resource (a .csv file), through a shortcode (table_csv.html), which is itself called in a .md page resource (section-3.md) for the page (index.md).

The shortcode works when I call it directly in index.md, but not when I call it in the page resource section-3.md.

Any hack existing for calling a page resource from another markdown page resource within the same page?

File structure:

/contents/en/module/module-3/

  • index.md
  • mycsv.csv
  • section-1.md
  • section-2.md
  • section-3.md

layouts/shortcodes/
-table_csv.html

{{ $csv := .Page.Resources.GetMatch "**.csv" }}
{{ $sep := "," }}
{{ $dataFile := getCSV $sep $csv.Permalink }}

<!-- your table building logic here -->
<table>
    <thead>
        <tr>
            <th colspan='2'>
                {{ $dataFile }}
            </th>
        </tr>
    </thead>
</table>

layouts/modules/list.html
Copying below the part of the code used to display the .md page resources in the index.md :

<!--Include .Content from the resource pages named section-*.md -->
{{ $sections := .Resources.Match "section-*.md" }}
{{ range $sections }}

    {{ $sectionClass := .Params.styling.sectionClass | default " "}}
    {{ $containerClass := .Params.styling.containerClass | default " "}}
    {{ $rowClass := .Params.styling.rowClass | default " "}}

  <section class="{{ $sectionClass }}">
    <div class="container {{ $containerClass }}">
      <div class="row {{ $rowClass }}">
            {{ .Content }}
          </div>
        </div>
      </div>
    </div>
  </section>
{{ end }}
<!-- End of Include .Content from the resource pages named section-*.md -->

Whenever I try to include the shortcode in a .md page resource, I get the nil pointer error, meaning the getMatch does not manage to search the file in the right place:

execute of template failed: template: shortcodes/table_csv.html:4:32: executing "shortcodes/table_csv.html" at <$csv.Permalink>: nil pointer evaluating resource.Resource.Permalink
content/en/modules/module-3/
β”œβ”€β”€ index.md      # This is a page. It has resources.
β”œβ”€β”€ mycsv.csv
β”œβ”€β”€ section-1.md  # This is a resource, not a page. It doesn't have resources.
β”œβ”€β”€ section-2.md  # This is a resource, not a page. It doesn't have resources.
└── section-3.md  # This is a resource, not a page. It doesn't have resources.

I am not sure that this should work, but it does:

{{ $csv := .Page.Parent.Resources.GetMatch "**.csv" }}

This seems like an undocumented/unintended behavior, given that (a) section-3.md is not a page, and (b) .Parent is a section method which should return β€œA section’s parent section or a page’s section.”, and (c) content/en/modules/module-3/index.md is a page, not a section.

If it were me, I would consider reorganizing the content in branch and leaf bundles:

content/en/modules/module-3/
β”œβ”€β”€ section-1/
β”‚   └── index.md
β”œβ”€β”€ section-2/
β”‚   └── index.md
β”œβ”€β”€ section-3/
β”‚   β”œβ”€β”€ index.md
β”‚   └── mycsv.csv
└── _index.md
1 Like

thanks @jmooring

This can be considered a working hack - for a specific user case.

In the end, the shortcode looks like this:

{{ $file := .Get `file` }}
{{ $sep :=  .Get `sep` }}

{{/* Attempt 1: shortcode called from a page which has resources */}}
{{ $csv := .Page.Resources.GetMatch $file }}
{{ if in .Page.Name "section-" }}
    {{/* Attempt 2: shortcode called from a resource page of a page with resources > hack: .Parent. */}}
    {{ $csv = .Page.Parent.Resources.GetMatch $file }} {{/* NOTE: it is =, not .= */}}
{{ end }}

{{ $dataFile := getCSV $sep $csv.Permalink }}

<!-- your table building logic here -->
<table class="table">
    {{ range $i, $r := (first 1 $dataFile) }} 
    <thead><tr>
       {{ range $j, $s := $r}}
        <th >{{ humanize $s }}</th>
       {{ end }}
    </tr></thead>
    {{ end }}
    {{ range $i, $r := (after 1 $dataFile) }}
       <tr>
       {{ range $j, $s := $r}}
          <td> {{ $s }} </td>
       {{ end }} 
       </tr> 
    {{ end }}      
</table>

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