Last active
March 5, 2018 22:50
-
-
Save danielhaim1/04d1b555c23826d64cb5409bb67290b3 to your computer and use it in GitHub Desktop.
Jekyll page.variables as operator in conditional statements w/ JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### data/integers.json | |
{ | |
"100": { "value": true } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
integers: | |
- "100" | |
- "200" | |
--- | |
{% assign json = site.data.integers %} | |
{{ json | inspect }} | |
{% for integer in page.integers %} | |
{% comment %} Here we cast our integer to a string, | |
as json keys are strings | |
(100 | append:"" => "100") | |
{% endcomment %} | |
{% assign intAsStr = integer | append:"" %} | |
{% comment %} as a possible json[intAsStr] returns an array, | |
we retrieve the first and only element in it | |
{% endcomment %} | |
{% assign data = json[intAsStr].first %} | |
{% if data["value"] == "true" %} | |
<h1>We have a match on {{ intAsStr }}</h1> | |
{% endif %} | |
{% endfor %} | |
### jekyll page: | |
{% assign json = site.data.integers %} | |
{{ json | inspect }} | |
{% for integer in page.integers %} | |
{% assign data = json[integer] %} | |
{% if data["value"] == true %} | |
<h1>We have a match on {{ integer }}</h1> | |
{% endif %} | |
{% endfor %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment