Asset helper complains about wrong spec
-
See the following playground here, I have a helper for picking what asset to draw, however jsreport complains about the param spec being wrong.
The first two each statements demonstrate that the helper returns a string as expected and that the assets can be found:
{{#each samples as |sample i|}} {{getLevelImage sample}} {{/each}} {{#each samples as |sample i|}} <img src="{#asset slyk-alert.svg @encoding=dataURI}" style="height:1.75%; margin: 0 auto;"> {{/each}}
Where the helper
getLevelImage
is:handlebars.registerHelper("getLevelImage", function(sample) { switch (sample.Results.Level) { case 1: return 'slyk-normal.svg @encoding=dataURI'; case 2: return 'slyk-warning.svg @encoding=dataURI'; case 3: return 'slyk-alert.svg @encoding=dataURI'; case 4: return 'slyk-critical.svg @encoding=dataURI'; default: return 'slyk-normal.svg @encoding=dataURI'; } });
This results in:
But the final line throws the error.
-
Handlebars does escaping if you call helper using
{{foo}}
. This breaks the asset specification. Try it with{#asset {{{getLevelImage sample}}}}
.FYI Are you aware that helpers in jsreport can be specified using global function? Calling
registerHelper
is not required.function getLevelImage(sample) { return 'foo' }
-
Of course, thanks Jan!
-
I did have another question, I have created the templates I needed using a jsreport-cli project, but I wanted to use jsreport in my existing node.js application. Do I need jsreport or to extend jsreport-core with the recipes and engine i want to use?
edit: Would you prefer I create a new post?
-
hi! you can use jsreport in your existing node.js application normally, this link would be helpful to you: https://jsreport.net/learn/adapting-jsreport , it contains section of how to integrate with express server in your app too.