Load image based on function value



  • I'm really new to jsreport and handlebars, so I'm hoping this is just a syntax thing. I've uploaded 12 images, and can include them in a report like this:

    <img src='{#image hero}' />
    

    However, I want to change the image based on the return value of a function. Used like this:

    {{#getImageName data}}{{/getImageName}}
    

    it correctly displays the required image name as plain text. Can I connect that function to the {#image} in some way?


  • administrators

    hi! maybe it is best if you return "{#image hero}" from your getImageName helper.

    example: https://playground.jsreport.net/studio/workspace/SkiFdulQZ/12


  • administrators

    also, we recommend using assets instead of images



  • Many thanks - yes, a function to return the entire string for the src tag works well. And assets is even better:

    <img src="{#asset {{getImageName this}}.jpeg @encoding=dataURI}" />
    

    I was confused by the add assets dialogue to begin with - I couldn't see how to add an image, and didn't realise the 'Upload' button didn't refer to 'styles.css'... Anyway, all sorted now.

    Thanks again!


Log in to reply
 

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