Assets images not displaying?

#1

Hi there!

Trying to use the assets here.

This works:

{{ if .Params.featured_image }}
   {{ $cat := .Params.categories | anchorize }}
   {{ $base := print .Section "/" $cat "/img" }}
   {{ $path := print .Params.featured_image }}
   {{ $url := print $base "/" $path }}

   {{ $image := resources.Get $url }}                                 
   {{ $scaled := $image.Fill "1200x630 center q85" }}  				
   {{ $scaled.Permalink }}

   <img src="{{ $scaled.Permalink }}">

{{ end }}

But this doesn’t:

<img src="{{ $image.Permalink }}">

Funny thing is, if I just print it out like this:

{{ $image.Permalink }}

It actually gives me the full path of the image, but there appears not to be any image there.

I can get to the image if I apply some kind of postprocess (Fill or Resize), but if I simply want to reach the image, it doesn’t gets exported from the assets folder on server build.

So I’m stuck at applying some random Resizing to all images in Assets (at the moment, 1200x q85) to get the images exported.

Any ideas how can I get the unchanged original image exported? (tried $scaled.Resize q85 but it still wants a width or height)

#2

I’ve found that this sometimes fails because of some image, not on the page you are currently editing, but on some other page. Probably, there is some resource that cannot be found somewhere else on the site. Cacheing also seems to play a role here (for instance, if you close the server and rebuild/relaunch the server, do you get a different error/behaviour?

#3

Within the resources context declare a variable to hold the dot i.e. {{ $original := . }} and then you simply call it like so {{ $original.Permalink }}

As for the other question regarding why the image doesn’t show up it sounds like an organization or a caching issue (like @HenrySkup said above) but difficult to identify without seeing some kind of a repo.

1 Like
#4

Can you elaborate a bit on this?

I tried to follow your logic there, which sounds good, but I haven’t been able to reproduce what you just said (within the resources context?)

#5

In your case, as things are at the moment (the resource is declared in a front matter parameter), the original image should be found in the above variable.

But for resources organized within a page bundle one could do something like this:

{{ $name = print $name "/*" }}
{{- with .Page.Resources.Match $name -}}
{{- range . -}}
{{ $original := . }}
{{- $resource := . -}}
{{- $thumb := $resource.Fill "400x400 smart" -}}
<a href="{{ $original.Permalink }}"><img src="{{ $thumb.Permalink }}" alt="something"></a>
{{ end -}}

That is what I meant above by resources context i.e. within the resources range.

In your case you could try something like

{{ with .Params.featured_image }}
{{ $original := . | absURL }}
...
{{ end }}

Also I just noticed that above you construct the PATH like so:

{{ $cat := .Params.categories | anchorize }}
   {{ $base := print .Section "/" $cat "/img" }}
   {{ $path := print .Params.featured_image }}
   {{ $url := print $base "/" $path }

Well does that image actually exist under "/.Section/.Params.categories/.Params.featured_image ?

It seems unlikely.

#6

I am saving it under "assets/.Section/.Params.categories/.Params.featured_image :wink:

For instance:

image: dice.jpg

assets/news/publishing/dice.jpg

The image is there!