|
| 1 | +--- |
| 2 | +title: Data Files |
| 3 | +description: Flatmark Data Files tutorial |
| 4 | +--- |
| 5 | + |
| 6 | +# {{page.title}} |
| 7 | + |
| 8 | +Data files are normal YAML files that you can use to store structured data for your site. |
| 9 | +You can use them in your pages and layouts to generate dynamic content. |
| 10 | + |
| 11 | +## Setting up data files |
| 12 | + |
| 13 | +Data files are stored in the `_data` folder inside your site folder. |
| 14 | +Create a file `authors.yaml` in the `_data` folder with the following content: |
| 15 | + |
| 16 | +```yaml |
| 17 | +- name: Sakib |
| 18 | + skills: [scala, java, python] |
| 19 | +- name: Senjin |
| 20 | + skills: [scala, java, javascript] |
| 21 | +``` |
| 22 | +
|
| 23 | +## Using data files in pages |
| 24 | +
|
| 25 | +You can use data files in your pages by using the `site.data` variable. |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | +For example, you can create a file `content/authors.md` with the following content: |
| 30 | + |
| 31 | +```markdown |
| 32 | +{% raw %} |
| 33 | +{{'{%'}} for author in site.data.authors %} |
| 34 | +- {{'{{'}} author.name }}, skills: {{'{{'}} author.skills|join(', ') }} |
| 35 | +{{'{%'}} endfor %} |
| 36 | +{% endraw %} |
| 37 | +``` |
| 38 | + |
| 39 | +This `for` loop goes through the array defined in `authors.yaml` and generates a list of authors. |
| 40 | +It is the same as if you wrote: |
| 41 | + |
| 42 | +```markdown |
| 43 | +- Sakib, skills: scala, java, python |
| 44 | +- Senjin, skills: scala, java, javascript |
| 45 | +``` |
| 46 | + |
| 47 | +You can take a look at |
| 48 | +[the example](https://github.com/sake92/flatmark/tree/main/examples/data-file) |
| 49 | +in GitHub for reference. |
| 50 | + |
| 51 | + |
0 commit comments