Using Thirdparty helper libraries with jsreport-core



  • I'm looking at the note on https://jsreport.net/learn/handlebars and I see that you can use third party helper libraries with jsreport, and I see the following example.

    var handlebars = require('handlebars');
    
    var HandlebarsIntl = require('handlebars-intl');
    HandlebarsIntl.registerWith(handlebars);
    

    I can see how to add helpers when using jsreport studio. What I am not sure about is how I could use this with jsreport-core ... would the fragment above go before the init()? after? I tried a few variations, but none of them made the helpers available within the template.

    Any pointers would be greatly welcomed.

    Thanks!



  • The jsreport-core code works. Does it help?

    var jsreport = require('jsreport-core')({
       tasks: {
           allowedModules: ['handlebars-intl']
       }
    })
    
    jsreport.init().then(function () {     
       return jsreport.render({
    	   template: {
    		  content: '{{formatNumber 10}}',
    		  engine: 'handlebars',
                      recipe: 'html',
                      helpers: `var HandlebarsIntl = require('handlebars-intl');
                       HandlebarsIntl.registerWith(handlebars); `
    	   }
    	}).then(function(resp) {	
         console.log(resp.content.toString())
       });
    }).catch(function(e) {
      console.log(e)
    })
    


  • Yes, I was missing the allowedModules as well as the idea that helpers: could be a string with all the code, let me play around with this as a start, thanks a ton!

    More generally, is there access to the handlebars object so I can work more directly with partials, and write more sophisticated helpers that are in my own separate module etc?

    Thanks again for your prompt and helpful response!



  • More generally, is there access to the handlebars object so I can work more directly with partials,

    The trouble here is that by default jsreport runs the templating engines isolated in the extra process. So we cannot expose handlebars object to you. It would work only if you use tasks.strategy='in-process'... I think we should be able to expose the handlebars instance in this case and make it easier for jsreport-core devs to extend it. I will discuss it with @bjrmatos .



  • Yes, that all makes sense. In the meanwhile, I realized I can just run handlebars separately and give the resulting HTML as the template.content ... at which point, we don't really need a templating engine to run within jsreport. In that case, is there a no-op or null templating engine I can use that just passes the input content to the next stage of rendering? I am currently setting it to jsrender which of course, does nothing because there are no tags left by the time it runs. I guess that's fine but I'm wondering if a no-op engine might be a bit more efficient?

    Thanks!



  • Yes, sure, doing the engines evaluation on your own is one of the options.

    The engine which does nothing is called none.


Log in to reply
 

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