Overriding YAML frontmatter variables from Liquid

Viewed 794

I would like my outermost Liquid template file to reference a JavaScript file only when the page requires it. I created a page variable, page.slideshow, which controls this:

default.html:

{% if page.slideshow %}
  <script type="text/javascript" src="/js/slideshow.js"></script>
{% endif %}

Now I have a template that "inherits" from default.html, which allows the page to specify in its YAML frontmatter a list of images (page.images). Only when this list has more than one element do I want slideshow.js to be included in the page.

My attempt at this looks something like:

other.html

---
layout: default
---
{% assign imagecount = page.images | size %}
{% if imagecount > 1 %}
  {% assign page.slideshow = true %}
{% endif %}

However, this does not seem to affect the rendering of default.html, because the script tag is not included in the final page.

As a workaround, I can modify the frontmatter of each page that lists multiple images to explicitly set slideshow: Yes, or change default.html to check page.slideshow or (page.images | size) > 0 but I'd prefer to structure it closer to what I attempted above.

1 Answers
Related