2

I would like to know if it's possible to add if statement in Front Matter to be selective about using attributes depending on page.url

The pages are generated from .md file virtually from a plugin using a base template so I need to add some conditions in Front Matter if possible.

This is a typical Front Matter

---
...
title: blah
description: blah blah

image: some-image.png
---

I want to do the following:

---
...
title: blah
description: blah blah

{% if page.url == "page1" %}

image: page1-image.png

{% else %}

image: general-image.png   

{% endif %}
---
Seong Lee
  • 10,314
  • 25
  • 68
  • 106

1 Answers1

4

Jekyll does not parse Liquid in Front Matter out of the box but there are plugins out there that can parse Liquid in Front Matter for you.

That said, I think your use-case can be easily solved within the page's "layout" itself..

{% capture image %}
  {% if page.url == "page1" %}
    page1-image.png
  {% else %}
    general-image.png
  {% endif %}
{% endcapture %}

{{ image | strip }}
ashmaroli
  • 5,209
  • 2
  • 14
  • 25
  • Thank you. So is `capture` a built-in Jekyll command that can inject value to Front Matter from a page layout? And what does `image | strip` do? – Seong Lee Jan 16 '18 at 22:58
  • `capture` is a block-type native Liquid tag that allows you to store the string value returned from evaluating Liquid within the tag body. The string will contain whitespace based on how you indent the enclosed Liquid constructs. This whitespace can then be removed via the `strip` filter.. – ashmaroli Jan 18 '18 at 08:38