blocks.md
Website
  • Introduction
  • Get started
  • How to use
  • Text input
  • Email input
  • URL input
  • Telephone input
  • Number input
  • Select box
  • Choice input
  • Picture choice
  • Rating input
  • Opinion scale / Net Promoter ScoreĀ®
  • Slides
  • Send responses
  • Class names and attributes
  • Google Sheets integration
  • Data-binding
  • Settings
  • Set and read data
  • Localize
  • WordPress plugin
Powered by GitBook
On this page
  • Set data to use on templates
  • Read data from remote sources (such as Google Sheets)
  • get-objects-name
  • JSON data and get-format

Set and read data

Learn how to set local data, and/or read data from Google Sheets or an API.

PreviousSettingsNextLocalize

Last updated 9 months ago

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.

``` data
{
  "name": "John Doe",
  "email": "john@example.com"
}
```

Hello, my name {{ name }} and my email address is {{ email }}.
Set data to use on template

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 %}
#! 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 %}

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}.

[
  {
    "id": 1,
    "name": "Leanne Graham",
    "username": "Bret",
    "email": "Sincere@april.biz",
    "address": {
      "street": "Kulas Light",
      "suite": "Apt. 556",
      "city": "Gwenborough",
      "zipcode": "92998-3874",
      "geo": {
        "lat": "-37.3159",
        "lng": "81.1496"
      }
    },
    "phone": "1-770-736-8031 x56442",
    "website": "hildegard.org",
    "company": {
      "name": "Romaguera-Crona",
      "catchPhrase": "Multi-layered client-server neural-net",
      "bs": "harness real-time e-markets"
    }
  },
  ...
]

Let's read this data and put it on the template:

#! get-format = json
#! get-objects-name = users
#! get-url = https://jsonplaceholder.typicode.com/users
#! page = single

{% for user in users %}
- **Name**: {{ user.name }}
- **Email**: {{ user.email }}
---
{% endfor %}
Set data using multiple data-blocks

Full documentation:

For example, here's we want to read:

Google Sheet we want to read
Data read from Google Sheets

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 we want to read:

Google Sheet we want to read
Table data read from Google Sheets

Here, we are reading TSV data by setting the #! get-format = tsv. Also note, because blocks.md uses , we are able to loop through the objects that we read from the spreadsheet and output the data on the template.

Let's say we want to read , which returns random JSON data in the following format:

JSON data read from API
Google Sheets integration
a Google Sheet
another spreadsheet
Nunjucks
https://jsonplaceholder.typicode.com/users