I’m trying to render a template with a conditional that checks to see if the content has a picture associated with it. These are in the archetype and content files as ‘image: “”’. If the string representing the image is empty the template should not render the div for the image. I tried doing this using isset to check and I currently have it in the template as follows:
{{ if isset .Params "image" }}
<div style="float:right;padding:10px">
<img src="{{ $baseurl }}img/{{ .Params.image }}" />
{{ if isset .Params "caption" }}
<p class="caption">
{{ .Params.caption }}
</p>
{{ end }}
</div>
{{ end }}
The strange thing is it works for the caption html - if there is no caption it does not render it and upon inspecting the element/html I can confirm it is not there. I am confused as to why the this template tag works for the caption but not the image.
I then tried changing it to the following but none of them work:
{{ if isset .Params "image" }}
<div style="float:right;padding:10px">
<img src="{{ $baseurl }}img/{{ .Params.image }}" />
{{ end }}
{{ if isset .Params "caption" }}
<p class="caption">
{{ .Params.caption }}
</p>
{{ end }}
{{ if isset .Params "image" }}
</div>
{{ end }}
Doesn’t compile:
{{ if .Params "image" != "" }}
{{ if .Params.image != "" }}
Which means that to be able to help you, I would need a “failing project”. If you could point to a GitHub project with this problem, that would help a lot.
Seems you’ve already solved your problem but, just in case anyone else is wrestling with similar functionality, here’s what I use in one of my templates, to conditionally show either a default icon or specific icon for a post –depending on whether or not the mdrposticon = "/path/to/iconimage.png" variable has been set in the front-matter.
<div class="posticon">
{{ if (not (isset .Params "mdrposticon")) | or (eq .Params.mdrposticon "") }}
<img src="/icons/default.png" />
{{ else }}
<img src="{{.Params.mdrposticon }}" />
{{ end }}
I had to add the additional test for eq .Params.mdrposticon "", as the parameter itself is always present in the front-matter. So, even when it was left blank: mdrposticon ="", the isset condition would always return true –which confused me for a while!