Set and read data
Learn how to set local data, and/or read data from Google Sheets or an API.
Set data to use on templates
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.
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.
Read data from remote sources (such as Google Sheets)
Full documentation: Google Sheets integration
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.
For example, here's a Google Sheet we want to read:
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:
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
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:
JSON data and get-format
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}
.
Let's say we want to read https://jsonplaceholder.typicode.com/users, which returns random JSON data in the following format:
Let's read this data and put it on the template:
Last updated