guigrpa / docx-templates

Template-based docx report creation
MIT License
904 stars 146 forks source link

How to use a nested dynamic variable as an argument for an image function? #278

Closed nabramow closed 2 years ago

nabramow commented 2 years ago

I'm trying to insert an image into my document, and the argument for that image function will be another dynamic variable.

For example, this is what I want to add to my docx:

{#IMAGE insertBase64String({#myNestedVariable#})#}

When I try to run this, I get an error "Error executing command: IMAGE insertImage( Unexpected end of input". I assume because myNestedVariable hasn't evaluated yet.

I want to be able to access this dynamic variable in my insertBase64String function here:

createReport({
        additionalJsContext: {
          insertBase64String: async (myNestedVariable) => {
           // I want to access the dynamic variable I passed in my docx here
            return {
              width,
              height,
              data,
              extension,
            };
          },
        },
      });

How can I do this? Do I have to create a separate variable with EXEC and reference it later in insertImage?

mathe42 commented 2 years ago

I would expect with code

createReport({
  data: {images: [/*...*/]},
  additionalJsContext: {
    insertBase64String: async (myNestedVariable) => {
      // Do something
    },
  },
});

and in template {#IMAGE insertBase64String(images[42])#}

this should work. It is near complete standard JS that is executed!

nabramow commented 2 years ago

Thank you! I ended up needing to add come code related to our own codebase to pick the inner argument up as a variable (we trigger some behavior with certain variables).