Help with conditional

In single.html I am trying to set up a conditional so that it looks through my category folder and runs the appropriate html depending on if value = yes or no. If value = yes, I want it to display as

<div class="photo-box u-1">
  <a href="{{.Params.website}}">
    <img src="{{.Params.thumbnail}}" alt="{{.Title}}">
  </a>
  <aside class="photo-box-caption">
    <span><a href="{{.Params.website}}">{{.Title}}</a></span>
  </aside>
</div>

If value = no, I want it to display as:

<div class="photo-box u-1">
  <iframe src="{{.Params.iframe}}" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
</div>

I am sure I’m doing something wrong as the conditional shows the wrong post and when viewing page source it shows empty html and does not show the other post:

<div class="photo-box u-1">
  <iframe src="https://category2.com" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
</div>

<div class="photo-box u-1">
  <iframe src="" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
</div>

Content - Category folder --> category-1.md

---
categories: []
date: 2016-02-27T20:32:41+01:00
description: ""
iframe: ""
tags: []
thumbnail: http://category.jpg
title: Category
type: ""
value: yes
website: http://www.category.com
---

Content - Category folder --> category-2.md

---
categories: []
date: 2016-02-27T20:35:41+01:00
description: ""
iframe: http://category2.com
tags: []
thumbnail: ""
title: Category 2
type: ""
value: no
website:
---

Layout --> Section --> category.html

  {{ range .Data.Pages }}
  {{ .Render "single"}}
  {{ end }}

_default --> single.html

{{ if eq .Params.value "yes" }}
<div class="photo-box u-1">
  <a href="{{.Params.website}}">
    <img src="{{.Params.thumbnail}}" alt="{{.Title}}">
  </a>
  <aside class="photo-box-caption">
    <span><a href="{{.Params.website}}">{{.Title}}</a></span>
  </aside>
</div>
{{else}}
<div class="photo-box u-1">
  <iframe src="{{.Params.iframe}}" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
</div>
{{end}}

Hi @charlie,

ad hoc I don’t see a mistake in your approach. Does Hugo output any errors on the command line?

Next, I would simplify your if-statements a bit. Instead of using strings like “yes” or “no” you could set to a boolean value (true or false). This way you can write:

{{ if .Params.value }}
    // include html for photobox
{{ else }}
    // use iframe
{{ end }}

Maybe you could give value a more descriptive name like use_iframe.

1 Like

Thanks! Works like a charm.