Load YAML data files into your roots project
CoffeeScript Makefile
Latest commit 40c2d21 May 12, 2016 Jeff Escalante Merge pull request #6 from FranzSkuffka/patch-1
Clarify documentation of data access in views
Permalink
Failed to load latest commit information.
lib add yaml view helper Nov 26, 2014
test
.editorconfig
.gitignore
.travis.yml update template Nov 20, 2014
Makefile
contributing.md initial commit Nov 20, 2014
license.md
package.json 0.0.5 May 18, 2015
readme.md Clarify documentation of data access in views May 10, 2016

readme.md

Roots-YAML

npm tests dependencies devDependencies Coverage Status

Load YAML data files into your roots project.

Note: This project is in early development, and versioning is a little different. Read this for more details.

Why Should You Care?

It's important to maintain clean DRY code. Instead of writing messy and duplicated markup, you can use the power of HTML precompilers and pass data into your templates.

Roots-YAML lets you store your site data in YAML, a human friendly data serialization format and then have that data exposed for you to use in your roots view templates.

Installation

  • make sure you are in your roots project directory
  • npm install roots-yaml --save
  • modify your app.coffee file to include the extension, as such
roots_yaml = require('roots-yaml')

module.exports =
  extensions:
    roots_yaml
      source: 'data'  # default value

Usage

Roots-YAML will look in your source directory and load any .yaml files into a data object for you to use in your views.

Each YAML file will be loaded using js-yaml and have the result set to data.<file_name>.

For example, if I have a directory data structured like this:

data/
  staff.yaml
  clients.yaml
  misc/
    doges.yaml
    manatoges.yaml

Inside my view templates I can use the following line to inspect the data object provided by roots-yaml:

pre=JSON.stringify(data, null, 2)

You will notice that, for example, data.staff contains the data in data/staff.yaml and data.misc.manatoges contains the data in data/misc/manatoges.

View Helper

Roots-YAML also adds a handy view helper to your Roots templates. Inside your views, add the following to insert a script tag setting a global data variable in your JS to your YAML data. You can also pass a string into yaml to change the name of the variable.

!= yaml()
!= yaml('manatoge')

results in:

<script>var data = {"doge":{"wow":"such yaml"}};</script>
<script>var manatoge = {"doge":{"wow":"such yaml"}};</script>

License & Contributing