Using lodash in helpers



  • I'd like to use some lodash functions to help prepare some data in the beforeRender() helper function. But I can't seem to get it to load via var _ = require('lodash'). I added it to the allowedModules array, and installed it in the main jsreport directory. I can use it in functions defined here:

    /jsreport/templates/CDL/helpers.js
    

    but not here, where it seems beforeRender() must be defined:

    /jsreport/data/scripts/CDL/content.js
    

    I tried moving the definition of beforeRender() to /jsreport/templates/CDL/helpers.js but then the whole thing times out. I'm presuming the beforeRender() function must be defined in the other location - still learning though, and not sure what the differences are in context and execution order!

    Simple question - is it possible to use external libraries like lodash in /jsreport/data/scripts/CDL/content.js?


  • administrators

    yes, it is possible to use external libraries, seems like you want to use lodash in a script

    NOTE: helpers and scripts are different, helpers are just functions that you can use in the template engine that you are using (handlebars, jsrender, jade, etc), think of it like normal handlebars helpers, scripts are functions that you can use for very different things outside of template rendering (that's why they have beforeRender and afterRender)

    here you have a configuration example:

    {
      ....
      "tasks": {
        ...
        "allowedModules": ["lodash"]
        ...
      },
      "scripts": {
        ....
        "allowedModules": ["lodash"]
        ...
      }
      ...
    }
    

    in the config you can see two different properties, tasks.allowedModules allows modules in helpers functions, and scripts.allowedModules allows modules in scripts functions, so i guess that you are not setting the value in scripts.allowedModules.

    seems like the documentation is not very clear about this (tasks.allowedModules is not even mentioned in the docs), i will add more notes to it.


Log in to reply
 

Looks like your connection to jsreport forum was lost, please wait while we try to reconnect.