Customize Built-in helpers (docx)



  • I will like to create my own function and use some Built-in helpers function.
    for example manipulate some html and return docxHtml, Is this possible ??

    function myHtml(data) {
        // some logic on data (html)
        return docxHtml (context=data) ; 
    }

  • administrators

    hi!

    generally speaking it depends, sometimes we check for the specific call in the document, so we expect the same name of the helper to be there. in the case of docxHtml we search for the exact call to happen in the document, so if you wrap it with your own, it wont work because we wont be able to find the call.

    what is your use case for this? what do you want to do in your own wrapper?



  • Here are my top 2 use cases :

    1. If the data is null docxHtml return error, so for start return null or blank and do not fail the entire document
    2. try to create html for a table from json and then embed it in the word template

  • administrators

    1. If the data is null docxHtml return error, so for start return null or blank and do not fail the entire document

    right now, if you pass null to the content hash parameter of docxHtml it throws an error, so you mean that you don't want that, right? you want instead for the render to continue with just an empty part. if that is the case you have two options that does not involve wrapping a helper.

    • you can normalize your data in a beforeRender script, just check your data fields there and set empty space '' directly there.
    • if you don't want to alter your data in script then you can call docxHtml with a handlebars subexpression like this {{docxHtml content=(normalizeEmpty html)}} without needing a helper wrapper. then you just need to create the normalizeEmpty
    function normalizeEmpty (value) {
      return value == null ? '' : value
    }
    

    try to create html for a table from json and then embed it in the word template

    i think for this case you can also use a handlebars subexpression, to do what you want, {{docxHtml content=(jsonToHtml json)}}, you just need to ensure your jsonToHtml helper takes the json and converts it to an HTML string and you are good to go.



  • too complex :)

    If i always need to check normalizeEmpty , it should be part of the base function here is an old rejected FR i asked
    docx: Add attribute to all docx helper - "enabled"

    I will like to do something like this: {{list2table somedata}}


  • administrators

    yes, i get that the template gets too verbose, but we always choose to throw error on invalid usage. this way you have the reason why something does not produce the expected output while developing the template.

    your use case is exactly why scripts exists, if there is some transformation or normalization you need to do to your data, beforeRender script is the place, and as far i can see your use case is well covered by just using the script.

    allowing to wrap the docxHtml will be a complex task, and not sure yet if possible, because we need to know the paragraph containing the docxHtml call, and without the convention of checking for an exact name like docxHtml we will need to take more complex approaches.


Log in to reply
 

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