Back

Technologies:

javascriptjavascript
vue.jsvue.js
avatar
Tolerim
a month ago

How do I ensure that placeholders are replaced within a docx template?

Currently, I am utilizing this library to replace the text placeholders in a docx template and generate multiple files. For the implementation of the front-end, I have utilized neutralino and vue, and I have created a method that allows the chosen docx file and the requisite data to be passed to the processing library. In order to ensure proper processing, I have utilized a for loop in the following manner:

// Combine all necessary data into an array
for (let i = 0; i < this.selectedData.length; i++) {
  this.dataPlaceholders.push({
    key1: val1,
    key2: val2
  });
}
// Call the library to generate documents

for (let i; i < this.dataPlaceholders.length; i++) {
  this.docxTemplate.process(template, this.dataPlaceholders[i]);
}
The data required to execute the library function is a combination of two arrays, and I am able to validate that they are in place with a console log. The documentation informs me that the placeholders are set using square brackets {}, and the name of each placeholder should be identical to the key in the dataPlaceholders array. However, upon testing, I have discovered that despite generating different documents, the placeholders are not being replaced, resulting in empty fields within the documents. How can I fix this so that the placeholders are replaced correctly?

Answers(2)
avatar
Vogi
a month ago
Verified Answer
It seems that the easy-template-x library uses a plugin model to support its various template manipulation capabilities. There are some built-in plugins and you can also write your own custom plugins if required. These are the plugins that come bundled with easy-template-x: Simple text replacement plugin. It is possible that the placeholders are not being replaced because the name of each placeholder should be identical to the key in the dataPlaceholders array. You may want to double-check that the names of your placeholders match the keys in your dataPlaceholders array. If you have already verified that your placeholders are named correctly, you may want to try using curly braces instead of square brackets for your placeholders. I hope this helps! Let me know if you have any other questions.

Sources

https://github.com/alonrbar/easy-template-x
avatar
Tolerim
a month ago
It seems that there is an issue with the way the placeholders are being defined in the docx template. Make sure that the placeholder names in the template match exactly with the keys in the dataPlaceholders array. Additionally, it looks like there is an error in the for loop for processing the documents. The variable i is not being assigned a value in the second for loop, which could be causing the issue. Consider changing it to let i = 0 or assigning a value to i before the loop. Here's an updated code snippet:
// putting all the desired data into a specific array
for(let i = 0; i < this.selectedData.length; i++){
  this.dataPlaceholders.push({
   key1: val1,
   key2: val2
  })
}

//call the library to get documents
for(let i = 0; i < this.dataPlaceholders.length; i++){
 this.docxTemplate.process(template, this.dataPlaceholders[i])
}
;