What am I not <Getting> about .Resources.Get

I just want the simplest working example of how to use 1 single image with assets. I have a single image in assets, and assets/images. Since nothing in the header worked for me, I have the following in my layout:

           <div class="card-image">    
              {{ $image := .Resources.GetMatch "tiebi.jpg" }}                                                                                    
              {{ printf "%#v" .Permalink }}    
              {{ printf "%#v" $image.RelPermalink }}    
                <img src="{{ $image.RelPermalink }}" alt="Placeholder image">    

I am getting the error: execute of template failed: template: index.html:63:36: executing “main” at <$image.RelPermalink>: nil pointer evaluating resource.Resource.RelPermalink

I have tried many variations such as:

.Resources.GetMatch "/tiebi.jpg"
.Resources.GetMatch "images/tiebi.jpg"

What do I need to make this work and move on?

.Resources.* is for Page Bundle assets.

File under assets/* is global resources, we use resources.Get. (this is the only available method to grab resource from global assets/* dir.

Yeah I had tried that too. I get:

execute of template failed: template: index.html:60:37: executing “main” at <.Resources.Get>: can’t evaluate field Get in type resource.Resources


execute of template failed: template: index.html:60:38: executing “main” at <.resources.Get>: resources is an unexported field of struct type page.Page

You need to wrap the above code within a condition that will execute it only if the $image variable exists:

              {{ $image := .Resources.Get "tiebi.jpg" }} 
              {{ with $image }}
                <img src="{{ $image.RelPermalink }}" alt="Placeholder image">    
              {{ end }}

Also I am not quite sure what you’re trying to do with:

              {{ printf "%#v" .Permalink }}    
              {{ printf "%#v" $image.RelPermalink }} 

The above is not really needed (unless you were trying to debug something).

.GetMarch fetches the first Page Resource file that matches a certain pattern. Typically a Page Resource resides under a Page Bundle.
.Resources.Get fetches a file that resides under the assetDir (and such a file is not a Page Resource).

Therefore you need to amend your code and use the latter method since you are not working with Page Resources in your template.

Also if the image is under assets/images/tiebi.jpg then you need to specify its parent folder relative to the assetDir i.e. .Resources.Get "/images/tiebi.jpg

resources.Get without the . dot prefix.

1 Like

Ok this was the issue. I guess I have to better understand the difference between .resources and resources. But the documentation here is not very friendly either. Yeah the rest of printing variables etc. was for troubleshooting. Since i was not getting any image, Thanks very much for the insight on resources!

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