Script interactions



  • This may be a weird scenario but i thought i would ask if it's possible. We have a template for Excel recipe generation (doesn't matter which we use). We also have a global JS file that has reusable methods in it for our other templates. Is it possible that the template can access variables in the global or more specifically, override the values?

    The idea we have is like the way CSS works, but we need it for style building in Excel. We have already created our own style builder and want to be able to extend it a bit more. We would like to have a variable in the global JS file like this

    let Styles = "{ blah blah blah }";
    

    and then in the template be able to Merge any additional styles on top of that So in the template we would do:

    Styles = object.assign(Styles, "{ my overrides or additional styles }");
    

    The issue we are having is being able to access the global properties from within the template itself.


  • administrators

    @zewar96 the variables a template can access come from data, so if you want to have some global variables available you just need to extend the data available.

    you can define a script and extend the data at runtime any way you want.

    async function beforeRender(req, res)  { 
      req.data = Object.assign({}, req.data, { Styles: {} // maybe define your styles or extend them here normally  })
    }
    

    is not this enough for you?



  • I may not be describing things correctly, but i am trying to access variables from within helpers, not scripts. Does that matter?

    I will work on getting you a playground entry to look at so you can see, but if the helpers can't run the beforeRender and don't have access to variables within the global, it won't really matter much


Log in to reply
 

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