2

I want to use a value in my frontmatter to specify a data file to loop through, but I can't get this to work.

I have a data file in _data/sidebars/sidebar1.yml. It contains:

- first
- second
- third

On a page I have this frontmatter:

---
title: My page
sidebar: site.data.sidebar.sidebar1
---

I want to use this code:

{% for entry in page.sidebar %}
* {{entry}}
{% endfor %}

However, this doesn't work. I've tried a number of things (involving assign and capture tags to define the page.sidebar content, but nothing seems to work).

The only thing that works is to do this:

{% if page.sidebar == "site.data.sidebars.sidebar1" %}
{% assign sidebar = site.data.sidebars.sidebar1 %}
{% endif %}

{% for entry in sidebar %}
* {{entry}}
{% endfor %}

However, I would like to avoid this extra code with the if statement, since it's easy to forget this and I would like to automate this more.

Any ideas on how to make this work?

Tom Johnson
  • 729
  • 8
  • 17
  • Since you have to assign the sidebar variable on the page itself anyway, can you use `{% for entry in site.data.sidebars.sidebar1 %}`? – approxiblue Dec 05 '16 at 02:03
  • In my scenario, different pages will have different sidebars. The layout itself will map the sidebar specified in the page layout to the correct sidebar in the _includes directory. I don't want to hard-code it like this. I need to pass in the sidebar name as a variable specified in the page's frontmatter. I tried to simplify the scenario in the original post so that I wouldn't overcomplicate things with details, but I really need to pass in the value from the frontmatter. – Tom Johnson Dec 05 '16 at 02:41
  • You cannot pass a collection reference to the front matter though. How about separate layouts for each sidebar? – approxiblue Dec 05 '16 at 02:49
  • Thanks for letting me know it's not possible. Creating separate layouts for each sidebar seems like more work than my workaround. Thanks, though. – Tom Johnson Dec 05 '16 at 05:11

1 Answers1

3

You have a typo in your front matter. It's :

sidebar: site.data.sidebars.sidebar1

not

sidebar: site.data.sidebar.sidebar1

You can even be less verbose.

In your front matter : sidebar: sidebar1

In your code :

{% assign sidebar = site.data.sidebars[page.sidebar]  %}

{% for entry in sidebar %}
  {{ entry | inspect }}
{% endfor %}
David Jacquel
  • 51,670
  • 6
  • 121
  • 147
  • 1
    David, if I wanted to add this documentation into the official Jekyll docs somewhere, how would you describe what's going on here? Something like, "In order to pass values from the page front matter into an assign variable, when the assigned variable isn't a string but rather a data reference, you must use brackets (instead of curly braces) to refer to the front matter's value."? Then provide the code example here. I don't have the right terminology to describe this. – Tom Johnson Dec 05 '16 at 19:09
  • 1
    Tom, you can find inspiration in liquid [documentation about expressions and variables](https://github.com/Shopify/liquid/wiki/Liquid-for-Designers#expressions-and-variables) or in this [SO answer about dot VS bracket notation](http://stackoverflow.com/a/4968448/1548376). – David Jacquel Dec 06 '16 at 12:18
  • Thanks for the reference. – Tom Johnson Dec 07 '16 at 17:48