Confusion regarding template conditionals and strings

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 != "" }}

I’m not sure what I am doing wrong.

Me, neither. It looks correct to me.

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.

Yes its on github.
The template it is in is friendorfoe > layouts > article > single
starting at line 36:

Thanks for taking a look at it! :slight_smile:

I would start by getting rid of these errors:

ERROR: 2015/11/07 template: partials/article_bottom_nav.html:6:30: executing "partials/article_bottom_nav.html" at <.NextInSection.Perma...>: nil pointer evaluating *hugolib.Page.Permalink in partials/article_bottom_nav.html
ERROR: 2015/11/07 template: partials/article_bottom_nav.html:3:30: executing "partials/article_bottom_nav.html" at <.PrevInSection.Perma...>: nil pointer evaluating *hugolib.Page.Permalink in partials/article_bottom_nav.html

Not saying it will solve your problem, but the errors above doesn’t help …

I fixed it by adding a different conditional check:

{{ if isset .Params “image” }}
{{ if .Params.image }}



{{ if isset .Params “caption” }}


{{ .Params.caption }}


{{ end }}
{{ end }}

{{ end }}

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!