Set local data that can be used in the template by putting valid JSON inside pairs of ``` or ~~~, with the data keyword placed right after the opening pair.
``` data
{
"name": "John Doe",
"email": "john@example.com"
}
```
Hello, my name {{ name }} and my email address is {{ email }}.
You can have multiple of these data-blocks anywhere on the document, and they will be combined together. In case of key collisions, the data-blocks that come after will override the ones that come before.
``` data
{
"name": "John Doe",
"email": "john@example.com"
}
```
Hello, my name **{{ name }}** and my email address is {{ email }}.
~~~ data
{
"name": "John"
}
~~~
Read data from remote sources (such as Google Sheets)
You can also read data from a URL using #! get-url = {url}. By default, CSV is the expected format, and the incoming data is formatted so that each key is a spreadsheet cell reference. This is because by default, Google Sheets is the expected source.
#! get-url = https://docs.google.com/spreadsheets/d/e/2PACX-1vTKJSALN8U91YSqvQZ6bQf24z0okzMM2J9D2VtptW2eASFbIC9dKyj2SlSpeaozczNR-u15mfpHqjuV/pub?gid=0&single=true&output=csv
#! page = single
``` data
{
"name": "John Doe",
"email": "john@example.com"
}
```
# {{ name }}
I'm a {{ A3 }} with {{ C3 }}+ years of experience, with a
strong focus on building scalable microservices that can
serve millions of users. I'm proficient in the following
languages:
- [.list-unstyled]
- [x] Python
- [x] JavaScript/TypeScript
- [x] Go
---
{% if D3 -%}
I'm currently available for work: {{ email }}.
{% else -%}
I'm currently not available for work.
{% endif %}
Apart from directly referencing each cell on your template, you can also go through the rows with a for-loop, especially if your spreadsheet is formatted like an SQL table (first row are the field names). Here's another spreadsheet we want to read:
#! get-url = https://docs.google.com/spreadsheets/d/e/2PACX-1vQBDvj-Sg6Ax2RA4bg74y-rUpm2jrCmulesVvr9JrXYFV7WvAK-V2I38P1NIApCf_UYZm3JZ-Tt8rfj/pub?gid=0&single=true&output=tsv
#! get-format = tsv
#! page = single
{% for person in objects %}
- **Name**: {{ person["Name"] }}
- **Twice the favorite number**: {{ person["Twice the favorite number"] }}
---
{% endfor %}
Here, we are reading TSV data by setting the #! get-format = tsv. Also note, because blocks.md uses Nunjucks, we are able to loop through the objects that we read from the spreadsheet and output the data on the template.
get-objects-name
As you can see above, the rows read from the remote source are available as objects in the template. However, you can change this reference name to anything you want using the #! get-objects-name = {name} setting. For example:
#! get-objects-name = persons
#! get-url = https://docs.google.com/spreadsheets/d/e/2PACX-1vQBDvj-Sg6Ax2RA4bg74y-rUpm2jrCmulesVvr9JrXYFV7WvAK-V2I38P1NIApCf_UYZm3JZ-Tt8rfj/pub?gid=0&single=true&output=csv
#! page = single
{% for person in persons %}
- **Name**: {{ person["Name"] }}
- **Twice the favorite number**: {{ person["Twice the favorite number"] }}
---
{% endfor %}
JSON data and get-format
So far, we've been only talking about reading CSV or TSV data from Google Sheets. However, JSON data from a traditional web API is also fully supported. You can read JSON data by setting #! get-format = JSON, placing your URL in the #! get-url = {url}, and (optionally) changing the objects reference using #! get-objects-name = {name}.