Reference List

On this page:

Scoping FTW

I will try to explain scoping. It is a simple concept, but hard to explain.

The most common example of scoping is browsing the folder structures on your computer when trying to find a file. In JSON, a variable that contains other variables is similar to a folder on your computer. When you click into a folder on your computer, you have access to the files inside. In a similar way, when you scope into a variable in JSON, you have direct access to the other variables inside.

Take the following JSON example:

{ 
    "items": [
      {
        "fullUrl": "/notebook/a-post-a-post",
        "title": "A Post! A Post!",
        "data": {
            "commentCount": 0,
            "body": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque tincidunt aliquam tortor eu volutpat. Sed sem mauris, faucibus a hendrerit non, vulputate non dolor. Morbi fermentum tortor et lectus ultrices vulputate. Morbi tincid boblong sipe..."
           }
      },
      {
        "fullUrl": "/notebook/blog-ideas",
        "title": "Blog Ideas",
        "data": {
            "commentCount": 0,
            "body": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque tincidunt aliquam tortor eu volutpat. Sed sem mauris, faucibus a hendrerit non, vulputate non dolor. Morbi fermentum tortor et lectus ultrices vulputate. Morbi rhoncus faucibus diam ..."
           }
      }
    ]
}

The variable "items" contains other variables. And inside "items", the variable "data" also contains other variables. Think of these as folders (or sections).

When you write out a section in JSON, it is like entering a folder, you have more direct access to the files within.

Ok. Now let's see JSON Template scoping in action:

Example:

If you want to add the "body" of each item, you could write:

{.repeated section items} 
  {data.body} 
{.end}

Or, you could scope into the "data" variable and do it this way:

{.repeated section items} 
  {.section data}
    {body}
  {.end}
{.end}

You can even scope down into the "body" variable and use the index character (@) to access the "body" variable (explained more on the basic syntax page):

{.repeated section items} 
  {.section data}
    {.section body}
      {@}
    {.end}
  {.end}
{.end}