Skip to content
This repository has been archived by the owner on Nov 29, 2017. It is now read-only.

documenting a bit more the parsing of yaml with liquid #266

Closed
baptistecalmes opened this issue Feb 5, 2015 · 15 comments
Closed

documenting a bit more the parsing of yaml with liquid #266

baptistecalmes opened this issue Feb 5, 2015 · 15 comments

Comments

@baptistecalmes
Copy link

First, I'm new to jekyll, and I think it's great.

I'm struggling a bit with the parsing of yaml files using for loops.

My understanding is that there are basically two types of objects that one might find in yaml files: lists and so-called "associative arrays" that are some kind of dictionaries. These two structures can be nested, and basically that is all there is to it. (See http://en.wikipedia.org/wiki/YAML)

I find it hard to guess the exact liquid syntax that can be used in jekyll as how to reach a particular element, and I find that the online documentation (which is great for most purposes) is a bit too scarce, unless I have missed some part of it.

So, my suggestion would be to:

  1. add more examples of how to parse a yaml file using liquid.
  2. give references to some kind of place where the types used in liquid are explained more precisely.
    This could be done by expanding the section:
    http://jekyllrb.com/docs/datafiles/#the-data-folder
    or by adding a new section.

I understand that this is borderline between liquid and jekyll, but it's such an important practical step to use the full power of jekyll that it definitely would help new users a lot if more details were given.

To make things concrete, here is a problem for which I only have an ugly solution, and I still wonder if its the best. Say one want to use the following yaml content in a file mylist.yaml

-firstkey: firstvalue
-secondkey: secondvalue

and you want to loop over the items of the list and access the keys and values. Then, the only solution I could find was to write

{% for thing in site.data.mylist %}
  {% for hash in thing %}
    {{hash[1]}}
    {{hash[0]}}
  {% endfor %}
{% endfor %}

and it took me a while to figure it out (it doesn't seem obvious that one needs to nest two for's, especially since the second one only ever loops through one iteration).

Another example: to actually use a variable in the description of the file name in the loop

{% for thing in site.data.myfile %}

one cas surround it in brackets, like:

{% for thing in site.data.[page.layout] %}

(which will loop in site.data.foo if page.layout is foo).
Again, it took me a while to figure out this syntax.
Etc.

Maybe I am missing some documentation somewhere, but I did do a good search before posting this suggestion.

Anyway, great job, keep up the good work ;-)

@doktorbro
Copy link
Member

Yaml feels very natural to everybody who is familiar with object-oriented languages. It’s very similar to Json. Thats why it is not documented here.

To make things concrete, here is a problem for which I only have an ugly solution

You show a solution. What is the problem you are trying to solve?

@baptistecalmes
Copy link
Author

I'm not trying to solve any problem.
I'm not complaining about documenting yaml.

I'm just saying that it would help new Jekyll users if the way liquid is used to parse yaml was more documented on the jekyll home page.

(and then I gave two examples of things that are not so easy to guess at first sight, from the documentation available)

@tomjoht
Copy link

tomjoht commented Feb 6, 2015

I'm also looking for more information about looping through YML values. Suppose I have 30 pages in my site and want to loop through them in a specific order as described in the YML file? It would be great to have a code sample showing how to do that.

@doktorbro
Copy link
Member

The Liquid creators have an introduction video on top of the official documentation.

Try to put this in your _config.yml:

numbers:
  - zero
  - one
  - two

Now you can loop over the site.numbers array:

{% for number in site.numbers %}
  {{ number | upcase }}
{% endfor %}

The result is:

ZERO
ONE
TWO

The array items are accessible over the index too.

{{ site.numbers[1] | upcase }}

#=> ONE

For more useful examples checkout the Jekyll Snippets. You can find a heavier Liquid usage in my Compress HTML layout.

@baptistecalmes
Copy link
Author

The official documentation of liquid does not mention yaml at all, and the parsing of yaml files with liquid is a feature that is implemented by Jekyll (or maybe by some ruby package it relies upon). It does not come from the original liquid specs.

Am I right, or am I missing something?

@doktorbro
Copy link
Member

doktorbro commented Feb 6, 2015 via email

@doktorbro
Copy link
Member

doktorbro commented Feb 6, 2015 via email

@baptistecalmes
Copy link
Author

I was afraid you'd say that ;-).

However, before offering some kind of contribution, I was hoping someone would point me to the right place that I had missed.

If no one else reacts, I'll think about it.

Thanks anyway for your time and suggestions.

@jaybe-jekyll
Copy link
Member

@penibelst Thanks for the concise examples and quality links. Made a note of the links to stay in tune with.

@mvaneijgen
Copy link

I have a problem like this, I am new and don't know where to get started or how to call what im looking for.

I have a data set in YAML looking something like this (there is 100+ of these sets)

- name: 
  image: 
  website:   
  dateadd: 
  releasedate: true|false

And I loop though them using

{% for data in site.data.games %}

But now I want to create a separate page where only the 'games' that do have a release date, so

releasedate: true

But I don't know where to start or how to call what im doing to get a proper Google search term, anyone tips?

here is the website btw if someone is interested

@rdyar
Copy link

rdyar commented Feb 18, 2015

mvaneijgen - I think you just need a where clause:

see http://jekyllrb.com/docs/templates/ for a little documentation. It should be easy to do.

Also, it looks like you are putting the data set in the config.yml? you can also put it in a data file (http://jekyllrb.com/docs/datafiles/) which I find very handy.

I find StackOverflow to be the best source of examples, that and random blogs. I think their is an infinite number of things that seem obvious to the one trying to implement it, but that creates a bit of a documentation problem.

@doktorbro
Copy link
Member

@mvaneijgen

{% assign released_games = site.data.games | where: "releasedate", true %}
{% for game in released_games %}
  {{ game.name }}
{% endfor %}

@mvaneijgen
Copy link

Friendly user on Reddit had pointed me to the solution

{% for data in site.data.games %}
   {% if games.releasedate == true %} Do stuff {% endif %}
{% endfor %}

This works for me

@doktorbro
Copy link
Member

@mvaneijgen This can not work. The iteration variable is data, not games.

{% for data in site.data.games %}
   {% if data.releasedate == true %} Do stuff {% endif %}
{% endfor %}

If you know about truthy and falsy in Liquid, you can simplify it.

{% for game in site.data.games %}
   {% if game.releasedate %} Do stuff {% endif %}
{% endfor %}

@parkr
Copy link
Member

parkr commented Jul 23, 2015

This repository is no longer maintained. Please search for your issue on Jekyll Talk, our new community forum. If it isn't there, feel free to post to the Help category and someone will assist you. Thanks!

@parkr parkr closed this as completed Jul 23, 2015
@jekyll jekyll locked and limited conversation to collaborators Jul 23, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants