JSON endpoints
By default views are looked up based on the controller action name +
.html.njk.
If an action name ends in JSON, the view is stilled looked up by controller
action name, but with .json.njk as a suffix.
By default outputted file names are created by taking the controller action
name and appending .html to it. If a slug is set in the data returned by a
controller action and that slug has an extension, the extension will be used
instead.
Putting this together:
//articles_controller.js
function indexJSON(ds) {
let articles = JSON.stringify(ds.article.instances, null, 2)
return responds({
articles: articles
}, {slug: 'index.json'})
}
would look for views/articles/indexJSON.json.njk as it's view template, and
would output articles/index.json as a file
if that template looked like:
{{ context.articles | safe }}it would produce a valid JSON file that could be consumed
Helpers
In order to make quick work of dealing with JSON data that you make create, we have a few helper modules:
getthat- load JSONattr-filter- filter through data by attributessearch-query- manage the search query in the browser URL