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 viavar _ = require('lodash')
. I added it to theallowedModules
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 thebeforeRender()
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
?
-
yes, it is possible to use external libraries, seems like you want to use
lodash
in a scriptNOTE: 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
andafterRender
)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, andscripts.allowedModules
allows modules in scripts functions, so i guess that you are not setting the value inscripts.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.