Accessing Specific Items From Data Files In Jekyll
Published:
Last modified:
Overview
Having a sample data file, we explore some of the options we have to access a specific item.
For the data file projects.yml
located in /_data/projects.yml
:
- project: funtime
url: www.funtime.url
description: This is really fun
- project: supertime
url: www.supertime.url
description: This is really super
How can we access the data item whose project name is funtime
?
Using an index
If we know the data item index, we can access them directly, in this case it is located in the position “0”:
{{site.data.projects[0]}}
Output:
{āprojectā=>āfuntimeā, āurlā=>āwww.funtime.urlā,ādescriptionā=>āThis is really funā}
Iterating
Iterating through all the data looking for an attribute
{% for item in site.data.projects %}
{% if item.project == "funtime" %}
{{item}}
{% endif %}
{% endfor %}
Outputs:
{āprojectā=>āfuntimeā, āurlā=>āwww.funtime.urlā, ādescriptionā=>āThis is really funā}
Changing the data file
The above data file can be optimized to make it more elegant and use the project name as their key:
funtime:
url: www.funtime.url
description: This is really fun
supertime:
url: www.supertime.url
description: This is really super
This way we can access each of the items directly:
{{site.data.projects['funtime']}}
So it would output:
{āurlā=>āwww.funtime.urlā, ādescriptionā=>āThis is really funā}
comments powered by Disqus
- Install Jekyll CMS on Ubuntu 18.10 in 3 steps THE RIGHT WAYJanuary 20, 2019
- What Are The Supported Language Highlighters In JekyllJanuary 25, 2017
- Accessing Specific Items From Data Files In Jekyll
- Jekyll Configuration File OptionsJune 12, 2016
- Understanding Jekyll PostsJune 12, 2016
- Jekyll basic conceptsMay 8, 2016
Jekyll Collections
- Jekyll Collections Versus PostsJuly 12, 2016
- Understanding How Collections WorkJune 11, 2016
Jekyll Templates
- Jekyll Variables and Liquid template tags cheatsheetJuly 13, 2016
Jekyll tutorials
- Multilingual Jekyll Without PluginsMay 8, 2017
- Host a Jekyll Website With Pretty Urls In Amazon S3 and CloudfrontApril 24, 2017
- Get A List Of Categories Based In Subfolders In JekyllMarch 3, 2017
- 5 Steps To Add Bootstrap 4 To Jekyll The Right WayFebruary 27, 2017
- Automated Deployment Of Jekyll Websites To Github Pages With A Git Push To GithubNovember 8, 2016
- How To Use Bower Scss With JekyllJune 18, 2016
- How to implement breadcrumbs on a Jekyll site with nested categoriesJune 7, 2016
- How To Handle Adsense In A Jekyll Development EnvironmentJune 6, 2016
- How To Prevent Content Displaying In A Jekyll Development EnvironmentJune 6, 2016
Articles
Subcategories
Except as otherwise noted, the content of this page is licensed under CC BY-NC-ND 4.0 . Terms and Policy.
Powered by SimpleIT Hugo Theme
·