Support and Documentation

Reusing data files

The illustration Styleguide project structure shows the recommended structure for a simple Styleguide project. You can promote the re-use of base data files by grouping them inside JSON arrays, one array per file. You store these files inside the directory styleguide/_group/, and then refer to the arrays' elements from the parent data files.

project-structure-group.svg

In the previous illustration, the data files Chefs.json and Recipes.json have a stanza that refers to a file /_group/Images.json, which is an array of similar template definitions. Chefs.json incorporates the first element in the array; Recipes.json incorporates the third element.

This modulariztion helps to easily update parent data files; if you modify one of the data files in the directory _group/, all the referring data files inherit the modifications.

The following steps illustrate how to create a collection of base data files and how to refer to them in parent data files.

Step 1: Create a library of related base data files

In the directory styleguide/_group/, create a JSON file as an array of similar data files.

Example 68. Images.json
[
  {
    "_template": "/path/Image.hbs",
    "src": "/otherPath/porridge-too-hot.png"
  },
  {
    "_template": "/path/Image.hbs",
    "src": "/otherPath/porridge-too-cold.png"
  },
  {
    "_template": "/path/Image.hbs",
    "src": "/otherPath/porridge-just-right.png"
  }
]


Step 2: Include base data file in parent

Include one of the base data files in your parent data file.

{
  "_template": "Goldilocks.hbs",
  "illustration": {
    "_include": "/_group/Images.json",
    "_key": 2
  }
}

In the previous snippet—

  • Line 2 indicates the associated template is Goldilocks.hbs.

  • Lines 4–5 indicate that illustration is populated by the third template defined in the file Images.json.

. See also: