config object shared between templates



  • Hey,

    Is there a way to declare an object, let's call it config (for performing logic in the templates), and import it to specific templates?

    Thank you



  • I'm not sure I get the question. Please elaborate more in the details.



  • If I want to have a config object like this

    const configColumns = [
    { 
       format: "string",
      columnType: "string"
    },
    { 
       format: "integer",
      columnType: "integer"
    }
    ]
    

    that will be available in a few different templates. where will I set that up? my assumption is that this different than global helper methods, right?



  • This can be one option
    https://playground.jsreport.net/w/anon/jikB0uQa

    Or you may get the asset in the helper and provide it to the template content through your custom helper
    See the options here
    https://jsreport.net/learn/assets



  • very interesting, thank you very much, I will play around this those.



  • I was trying to implement the first option you suggested but I am having some trouble:

    1. How should I be creating this config file? If I am using the Studio UI- am I creating a new asset?

    2. Once the config file is created, how can I import it in a handlebar template? I have tried different syntax but handlerbar is not accepting any of the options from the docs {{#asset “config.js” “utf8”}} or {{asset “config.js” “utf8“}} . Also tried to require it in the script but jsreports.assets is undefined. Tried to install jsreports-assets but that did not help.

    I will really appreciate any guidance here.
    Thank you



  • yes, that config.json entity is an asset and you can get it to the template's script tag as it's in the playground demo

    <script>
        const config = {{asset "config.json" "utf8"}}   
    </script>
    
    


  • I appreciate your help and the very fast response.

    It could be that my setup is not right, or the version or something. I have been playing around with both options, and I can not get the data to exist in the template. Either it is blank or the template fails with parsing errors or undefined.

    I really do appreciate your help, I will keep looking for a solution.



  • The example I posted works only jsreport >=v3
    So it will be the best to have the latest jsreport 3.1.1


Log in to reply
 

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