.Resources.GetMatch not recognizing resource files in bundle

Hi guys, I have a section called fooSec, and under it a fooPost leaf bundle, file structures as below

-content
     -fooSec
         -fooPost
             -index.md
             -my.css
             -my.js

Then, the front matter of index.md looks like

 +++
date="2020-02-15"
title="someTitle"

[[resources]]
  name = "my.js"
  src = "my.js"

[[resources]]
  name = "my.css"
  src = "my.css"
+++

Now, I have a partial template file, trying to populate the css content and js content into the fooPost, template looks like:

<style>{{ (.Resources.GetMatch "my.css").Content }}</style>
<script>{{ (.Resources.GetMatch "my.js").Content | safeJS }}</script>

But it keeps throwing me error like below:

execute of template failed: template: partials/foocontent.html:1:31: executing "partials/fooSec/foocontent.html" at <"my.css">: nil pointer evaluating resource.Resource.Content

I tried use "*" or "*.css", but got the same error, also I tried remove .Content and simply test .Resources.GetMatch, still same error.

So it seems the js and css resource files are not recognized, any ideas where I’m doing wrong? Thanks in advance.

You probably have a page using the same layout that does not have those .Resources, which means that the .GetMatch would return a nil.

I suggest wrapping in a with:

{{ with (.Resources.GetMatch "my.css") }}
{{ .Content }}
{{ end }}
1 Like

Hi @pointyfar, thanks for the suggestion.

You probably have a page using the same layout that does not have those .Resources, which means that the .GetMatch would return a nil.

As fooSec is a test section, there’s only fooPost in it, and the partial template resides in layout/partial/fooSec/, so it is applied only to pages in that section, hence I am sure your suggested case does not exist in my scenario here

I suggest wrapping in a with :

Wrapping in a with clause will check nill first, thus “remove” the error notice from hugo, but still not resolving the problem (or explaining the reason) of .Resources.GetMatch not recognizing my files.

Do you have your site code somewhere we can clone from? That would make it easier to help.

1 Like

@pointyfar, thanks again. Taking your advice and I try to build up a skeleton mockup site to showcase my code, and in the process I find out where the problem is:

I was actually calling the partial from inside the list page template for fooSec, and that’s why .Resources.GetMatch found nothing about my.css and my.js, which should be accessed from fooSec’s single page template.

1 Like

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