I have
config.toml
[params]
imgThemeDir = 'images/'
and
\themes\mytheme\layouts\partials\block_title.html
<img src='{{ .Site.Params.imgThemeDir }}{{ .img }}' />
This
{{ $img := "img.jpg" }}
{{ partial "block_title" ( dict "img" $img ) }}
renders
<img src='img.jpg' />
without ‘images/’
Why?
I tried
{{ partial "block_title" ( dict "img" $img "context" . ) }}
and
<img src='{{ .context.Site.Params.imgThemeDir }}{{ .img }}' />
But It also doesn’t work.
I think you need to pass in the context to your partial, something like:
{{ partial "block_title" ( dict "img" $img ) . }}
I had issues not getting site params rendered in my partials once I did that it worked, good luck.
Thank you for your reply. But it also don’t work.
Did you try debugging without the img var?
{{ partial "block_title" . }}
Just to check if it’s actually passing in the context to the partial.
Also try debugging inside the partial like it says in the docs: https://gohugo.io/templates/debugging
Just to make sure the partial is getting values, good luck.
I tried this. Without dict () {{ .Site.Params.imgThemeDir }} works.
What is dict supposed to do?
I think you can just pass in the img var after the context.
{{ partial "block_title" . $img }}
Or you can probably concatenate the site params and the img var and pass that value into the partial.
1 Like
It’s doesn’t work.
It’s the solution. Thank you.
But I would be undestand why {{ .Site.Params.imgThemeDir }} doesn’t work with dict.
I tried
{{ partial "block_title" ( dict "img" $img "context" . ) }}
and
<img src='{{ .context.Site.Params.imgThemeDir }}{{ .img }}' />
But It also doesn’t work.
I wish I could give you an answer but my Go knowledge is very limited I just started using hugo 3 weeks ago myself and I’m still learning :-/
Glad my suggestion helped you solve your problem, hopefully someone with more experience can provide more details as to why what you were trying doesn’t work.
1 Like
Context matters. The context inside the partial is defined by what you pass to it. You can’t just reference .Site
from inside a partial and assume it will work. A partial can only receive a single parameter, so you’re doing well by passing a dictionary/map.
# Pass in the theme directory as an item in the dictionary param to the
{{ $img := "img.jpg" }}
{{ partial "block_title" ( dict "dir" .Site.Params.imgThemeDir "img" $img ) }}
Then
<img src='{{ .dir }}{{ .img }}' />
1 Like
This is what solved it for me, thanks!