0

This is one of those questions that have been asked a few times but still has no good answers. I cant work out how to retrieve my data in stored in seperate json files for use by the variables i define in my partials and macros. Note I am trying to achieve this without gulp or grunt, just node if if possible.

Finding a clear solution has been very difficult. Can anyone assist?

JPB
  • 592
  • 2
  • 6
  • 28
  • 1
    Did you try something as simple as this `res.render('view-name', {my_variable: require('my_json_file.json'));` and in nunjucks use `

    {{ my_variable }}

    `
    – Molda Jan 08 '17 at 19:09
  • No will try it out. Thanks. – JPB Jan 10 '17 at 03:53
  • @molda I'm having trouble understanding how to use this. If I have a json with 20 variables and I want to be able to use any of those variables in any view how would I make them globally available? For example something like: res.render('view1.njk', {all-the-variables: require('variables.json'));res.render('view2.njk', {all-the-variables: require('variables.json')); then in view1.njk:

    {{var1}}

    {{var2}}

    {{var3}}

    and view2.njk

    {{var1}}

    – JPB Jan 23 '17 at 05:11
  • 1
    Assuming variables.json looks like this {"val1": "hello"} render -> res.render('view2.njk', {vars: require('variables.json')}); the view ->

    {{ vars.val1 }}

    – Molda Jan 23 '17 at 15:14
  • cheers mate will give it a go. just woke up. how the heck do you get started with this it seems like theres no info out there! Ive got an open question about this, if you want to anawer it I can mark you right? http://stackoverflow.com/questions/41800188/nunjucks-retrieve-data-from-variables-in-json-file-npm-only – JPB Jan 24 '17 at 00:08
  • @molda 'router.get('/', function(req, res) { res.render('index.njk', {vars: require('vars.json')}); });' result in browser: Error: template not found: error.nunjucks. When I remove the vars:require part it works fine? – JPB Jan 24 '17 at 02:34
  • FINALLY GOT IT!!!! YES. THANK YOU. My file path was incorrect : P – JPB Jan 24 '17 at 03:23

0 Answers0