Ok.
The most simple setup:
This is your image: /static/images/my-image.jpg
.
In your markdown file:
---
hero_image: /images/my-image.jpg
---
This is the way to access to access the image in a template file using relLangURL
you mentioned:
{{ with .Params.hero_image }}
<img src="{{ . | relLangURL }}" alt=" ">
{{ end }}
The dot is replaced by your image!
Actually you do not need relLangURL
here as images from /static/
are just copied to your /public/
folder.
{{ with .Params.hero_image }}
<img src="{{ . }}" alt=" ">
{{ end }}
Is this a solution already? I am not sure how your content is structured and why you need relLangURL
.
Let me try to explain two completely different concepts of dealing with images using Hugo
1)
Images that live anywhere in /static/
are not processed by Hugo, that is they are just copied to the corresponding folder into public
. On the server they get a fix URL.
Example: /static/images/my-image.jpg
will be copied to /images/my-image.jpg
.
You have access to this image
-
using markdown in the content part of your markdown file, e.g.
![My alt text](/images/my-image.jpg)
or
-
in the template files (for example /layouts/_default/baseof.html
) as already posted.
2)
If you want to manipulate your image using Hugo (resize, blur …) and keep text content and images together(!) your images live somewhere in your /content/
folder. (An alternative is the /assets/
folder.)
My preference is to always keep text content and images together (unless the same image is used in multiple places).
Hero images are a good example for using this method: They are always placed in the same position on each page, they belong to a single page or article (and resizing makes sense to limit the image sizes or to even make responsive images using multiple sizes in the same place).
Of course, you have access to these images using markdown, too.
Advantages: You can use Hugo’s excellent image processing possibilities, you can setup a real multilingual site using different (hero) images for each language (as mentioned here).
So you first have to decide whether 1) or 2) is a better fit. If in doubt I would always suggest 2), but this is opinionated.
A suggestion for testing a hero and content part image:
Place an image here: /content/about/my-image.jpg
.
Put a markdown file into the content folder --> /content/about/index.md
. Content:
---
title: About
hero_image: my-image.jpg
---
My about page content.
![My unprocessed hero image](my-image.jpg)
Put
<html>
<body>
<h1>{{ .Title }}</h1>
{{ with .Params.hero_image }}
{{ $image := $.Resources.GetMatch . }}
{{ $image = $image.Resize "1200x" }}
<img src="{{ $image.Permalink }}">
{{ end }}
{{ .Content }}
</body>
</html>
into a template file, e.g. /layouts/_default/baseof.html
.
There will be to images on the page:
- a dynamically resized, ‘real’ hero image, and
- the original image from the markdown content.