We'll see some example "helpers" which access the vocabs, without needing lookup
, or passing in @root.vocab
.
Lets use the following vocabs:
key | english |
---|---|
country_names_1 | England |
country_names_2 | Scotland |
country_names_3 | Wales |
country_names_4 | Northern Ireland |
And let's see the helpers we can use to access its data:
the HTML:
the helper:
const vocab = (key, options) => {
const { vocab } = options.data.root;
return vocab[key];
};
module.exports = vocab;
the HTML:
the helper:
const vocabKeys = (pattern, options) => {
const { vocab } = options.data.root;
const keys = Object.keys(vocab).filter(key => (key.includes(pattern)));
return keys;
};
module.exports = vocabKeys;
the HTML
the helper:
const vocabValues = (pattern, options) => {
const { vocab } = options.data.root;
const keys = Object.keys(vocab).filter(key => (key.includes(pattern)));
let values = [];
keys.forEach(key => {
values.push(vocab[key]);
});
return values;
};
module.exports = vocabValues;
the HTML
the helper
const vocabs = (pattern, options) => {
const { vocab } = options.data.root;
const keys = Object.keys(vocab).filter(key => (key.includes(pattern)));
let obj = {};
keys.forEach(key => {
obj[key] = vocab[key];
});
return obj;
};
module.exports = vocabs;
the HTML
the helper
const assign = (varName, varValue, options) => {
if (!options.data.root) {
throw Error('options.data.root does not exist');
}
options.data.root[varName] = varValue;
}
module.exports = assign;
the HTML
the helper:
const assetPath = (concat, options) => {
return options.data.root.assetsPath + concat + '?ticker=' + options.data.root.version + Date.now();
}
module.exports = assetPath;