Skip to content

Commit 3b335ac

Browse files
committed
Add data-files example
1 parent 20d997b commit 3b335ac

4 files changed

Lines changed: 68 additions & 0 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+

docs/content/tutorials/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pagination:
1111
set tutorials = [
1212
{ "label": "Quickstart", "url": "/tutorials/quickstart.html" },
1313
{ "label": "Multilingual Site", "url": "/tutorials/multilang.html" },
14+
{ "label": "Data Files", "url": "/tutorials/data-files.html" },
1415
{ "label": "Custom Local Theme", "url": "/tutorials/custom_local_theme.html" }
1516
]
1617
%}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
- name: Sakib
3+
skills: [scala, java, python]
4+
- name: Senjin
5+
skills: [scala, java, javascript]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
title: Home
3+
description: Home page
4+
---
5+
6+
# Hello Flatmark!
7+
8+
{% for author in site.data.authors %}
9+
- {{ author.name }}, skills: {{ author.skills|join(', ') }}
10+
{% endfor %}
11+

0 commit comments

Comments
 (0)