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:

    0_1522636840186_upload-15361a6e-73d3-4ff8-85d7-d8dab0e74183
    But the final line throws the error.
    0_1522636889324_upload-b41d0c60-9ef7-4031-a5ff-7c553d06aac4



  • 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?


  • administrators

    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.


Log in to reply
 

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