Frequently asked questions

Inserting new lines

const doc = new Docxtemplater(zip, { linebreaks: true });

then in your data, if a string contains a newline, it will be translated to a linebreak in the document.

Insert HTML formatted text

It is possible to insert HTML formatted text using the HTML pro module

Generate smaller docx using compression

The size of the docx output can be big, in the case where you generate the zip the following way:

doc.getZip().generate({ type: "nodebuffer"})

This is because the zip will not be compressed in that case. To force the compression (which could be slow because it is running in JS for files bigger than 10 MB)

const zip = doc.getZip().generate({
        type: "nodebuffer",
        compression: "DEFLATE"
});

Writing if else

To write if/else, see the documentation on sections for if and inverted sections for else.

Using boolean operators (AND, OR) and comparison operators (<, >)

You can also have conditions with comparison operators (< and >), or boolean operators (&& and ||) using angular parser conditions.

Conditional Formatting

With the PRO styling module it is possible to have a table cell be styled depending on a given condition (for example).

Using data filters

You might want to be able to show data a bit differently for each template. For this, you can use the angular parser and the filters functionality.

For example, if a user wants to put something in uppercase, you could write in your template:

{ user.name | uppercase }

See angular parser for comprehensive documentation

Keep placeholders that don’t have data

It is possible to define which value to show when a tag resolves to undefined or null (for example when no data is present for that value).

For example, with the following template

Hello {name}, your hobby is {hobby}.
{
    "hobby": "football",
}

The default behavior is to return “undefined” for empty values.

Hello undefined, your hobby is football.

You can customize this to either return another string, or return the name of the tag itself, so that it will show:

Hello {name}, your hobby is football.

It is possible to customize the value that will be shown for {name} by using the nullGetter option. In the following case, it will return “{name}”, hence it will keep the placeholder {name} if the value does not exist.

function nullGetter(part, scopeManager) {
    /*
        If the template is {#users}{name}{/} and a value is undefined on the
        name property:

        - part.value will be the string "name"
        - scopeManager.scopePath will be ["users"] (for nested loops, you would have multiple values in this array, for example one could have ["companies", "users"])
        - scopeManager.scopePathItem will be equal to the array [2] if
          this happens for the third user in the array.
        - part.module would be empty in this case, but it could be "loop",
          "rawxml", or or any other module name that you use.
    */

    if (!part.module) {
        // part.value contains the content of the tag, eg "name" in our example
        // By returning '{' and part.value and '}', it will actually do no replacement in reality. You could also return the empty string if you prefered.
        return '{' + part.value + '}';
    }
    if (part.module === "rawxml") {
        return "";
    }
    return "";
}
const doc = new Docxtemplater(zip, {nullGetter: nullGetter});

Performance

Docxtemplater is quite fast, for a pretty complex 50 page document, it can generate 250 output of those documents in 44 seconds, which is about 180ms per document.

There is also an interesting blog article at https://javascript-ninja.fr/optimizing-speed-in-node-js/ that explains how I optimized loops in docxtemplater.

Support for IE9 and lower

docxtemplater should work on almost all browsers: IE7+, Safari, Chrome, Opera, Firefox.

The only ‘problem’ is to load the binary file into the browser. This is not in docxtemplater’s scope, but here is the recommended code to load the zip from the browser:

https://github.com/open-xml-templating/pizzip/blob/master/documentation/howto/read_zip.md

The following code should load the binary content on all browsers:

PizZipUtils.getBinaryContent('path/to/content.zip', function(err, data) {
  if(err) {
    throw err; // or handle err
  }

  const zip = new PizZip(data);
});

Get list of placeholders

To be able to construct a form dynamically or to validate the document beforehand, it can be useful to get access to all placeholders defined in a given template. Before rendering a document, docxtemplater parses the Word document into a compiled form. In this compiled form, the document is stored in an AST which contains all the necessary information to get the list of the variables and list them in a JSON object.

With the simple inspection module, it is possible to get this compiled form and show the list of tags. suite:

const InspectModule = require("docxtemplater/js/inspect-module");
const iModule = InspectModule();
const doc = new Docxtemplater(zip, { modules: [iModule] });
const tags = iModule.getAllTags();
console.log(tags);
// After getting the tags, you can optionally set some data and render the document like this:
// doc.setData(data);
// doc.render();

With the following template:

{company}

{#users}
{name}
{age}
{/users}

It will log this object:

{
    "company": {},
    "users": {
        "name": {},
        "age": {},
    },
}

You can also get a more detailled tree by using:

console.log(iModule.fullInspected["word/document.xml"]);

The code of the inspect-module is very simple, and can be found here: https://github.com/open-xml-templating/docxtemplater/blob/master/es6/inspect-module.js

Convert to PDF

It is not possible to convert docx to PDF with docxtemplater, because docxtemplater is a templating engine and doesn’t know how to render a given document. There are many tools to do this conversion.

The first one is to use libreoffice headless, which permits you to generate a PDF from a docx document:

You just have to run:

libreoffice --headless --convert-to pdf --outdir . input.docx

This will convert the input.docx file into input.pdf file.

The rendering is not 100% perfect, since it uses libreoffice and not microsoft word. If you just want to render some preview of a docx, I think this is a possible choice. You can do it from within your application by executing a process, it is not the most beautiful solution but it works.

If you want something that does the rendering better, I think you should use some specialized software. PDFtron is one of them, I haven’t used it myself, but I know that some of the users of docxtemplater use it. (I’m not affiliated to PDFtron in any way).

Pptx support

Docxtemplater handles pptx files without any special configuration (since version 3.0.4).

It does so by looking at the content of the “[Content_Types].xml” file and by looking at some docx/pptx specific content types.

My document is corrupted, what should I do ?

If you are inserting multiple images inside a loop, it is possible that word cannot handle the docPr attributes correctly. You can try to add the following code before instantiating the Docxtemplater instance.

const fixDocPrCorruptionModule = {
    set(options) {
        if (options.Lexer) {
            this.Lexer = options.Lexer;
        }
        if (options.zip) {
            this.zip = options.zip;
        }
    },
    on(event) {
        if (eventName === "attached") {
            this.attached = false;
        }
        if (event !== "syncing-zip") {
            return;
        }
        const zip = this.zip;
        const Lexer = this.Lexer;
        let prId = 1;
        function setSingleAttribute(partValue, attr, attrValue) {
            const regex = new RegExp(`(<.* ${attr}=")([^"]+)(".*)$`);
            if (regex.test(partValue)) {
                return partValue.replace(regex, `$1${attrValue}$3`);
            }
            let end = partValue.lastIndexOf("/>");
            if (end === -1) {
                end = partValue.lastIndexOf(">");
            }
            return (
                partValue.substr(0, end) +
                ` ${attr}="${attrValue}"` +
                partValue.substr(end)
            );
        }
        zip.file(/\.xml$/).forEach(function (f) {
            let text = f.asText();
            const xmllexed = Lexer.xmlparse(text, {
                text: [],
                other: ["wp:docPr"],
            });
            if (xmllexed.length > 1) {
                text = xmllexed.reduce(function (fullText, part) {
                    if (
                        part.tag === "wp:docPr" &&
                        ["start", "selfclosing"].indexOf(part.position) !== -1
                    ) {
                        return (
                            fullText +
                            setSingleAttribute(part.value, "id", prId++)
                        );
                    }
                    return fullText + part.value;
                }, "");
            }
            zip.file(f.name, text);
        });
    },
};
const doc = new Docxtemplater(zip, { modules: [fixDocPrCorruptionModule] });

Attaching modules for extra functionality

If you have created or have access to docxtemplater PRO modules, you can attach them with the following code:

const doc = new Docxtemplater(zip, { paragraphLoop: true, linebreaks: true, modules: [...] });
doc.setData(data);

Ternaries are not working well with angular-parser

There is a common issue which is to use ternary on scopes that are not the current scope, which makes the ternary appear as if it always showed the second option.

For example, with following data:

doc.setData({
   user: {
      gender: 'F',
      name: "Mary",
      hobbies: [{
         name: 'play football',
      },{
         name: 'read books',
      }]
   }
})

And by using the following template:

{#user}
{name} is a kind person.

{#hobbies}
- {gender == 'F' : 'She' : 'He'} likes to {name}
{/hobbies}
{/}

This will print:

Mary is a kind person.

- He likes to play football
- He likes to read books

Note that the pronoun “He” is used instead of “She”.

The reason for this behavior is that the {gender == ‘F’ : “She” : “He”} expression is evaluating in the scope of hobby, where gender does not even exist. Since the condtion gender == ‘F’ is false (since gender is undefined), the return value is “He”. However, in the scope of the hobby, we do not know the gender so the return value should be null.

We can instead write a custom filter that will return “She” if the input is “F”, “He” if the input is “M”, and null if the input is anything else.

The code would look like this:

expressions.filters.pronoun = function(input) {
  if(input === "F") {
     return "She";
  }
  if(input === "M") {
     return "He";
  }
  return null;
}

And use the following in your template:

{#user}
{name} is a kind person.

{#hobbies}
- {gender | pronoun} likes to {name}
{/hobbies}
{/}

Multi scope expressions do not work with the angularParser

If you would like to have multi-scope expressions with the angularparser, for example:

You would like to do: {#users}{ date - age }{/users}, where date is in the “global scope”, and age in the subscope users, as in the following data:

{
  "date": 2019,
  "users": [
    {
      "name": "John",
      "age": 44
    },
    {
      "name": "Mary",
      "age": 22
    }
  ]
}

You can make use of a feature of the angularParser and the fact that docxtemplater gives you access to the whole scopeList.

// Please make sure to use angular-expressions 1.1.2 or later
// More detail at https://github.com/open-xml-templating/docxtemplater/issues/589
const expressions = require("angular-expressions");
const assign = require("lodash/assign");
function angularParser(tag) {
   if (tag === ".") {
      return {
         get(s) {
            return s;
         },
      };
   }
   const expr = expressions.compile(
       tag.replace(/(’|‘)/g, "'").replace(/(“|”)/g, '"')
   );
   return {
      get(scope, context) {
         let obj = {};
         const scopeList = context.scopeList;
         const num = context.num;
         for (let i = 0, len = num + 1; i < len; i++) {
             obj = assign(obj, scopeList[i]);
         }
         return expr(scope, obj);
      },
   };
}

const doc = new Docxtemplater(zip, {parser: angularParser});

Access to XMLHttpRequest at file.docx from origin ‘null’ has been blocked by CORS policy

This happens if you use the HTML sample script but are not using a webserver.

If your browser window shows a URL starting with file://, then you are not using a webserver, but the filesystem itself.

For security reasons, the browsers don’t let you load files from the local file system.

To do this, you have to setup a small web server.

The simplest way of starting a webserver is to run following command:

npx http-server
# if you don't have npx, you can also do:
# npm install -g http-server && http-server .

On your production server, you should probably use a more robust webserver such as nginx, or any webserver that you are currently using for static files.

Docxtemplater in a React project

There is an online react demo available on stackblitz.

You can use the following code:

import React, { Component } from 'react';
import React from 'react';
import Docxtemplater from 'docxtemplater';
import PizZip from 'pizzip';
import PizZipUtils from 'pizzip/utils/index.js';
import { saveAs } from 'file-saver';

function loadFile(url, callback) {
  PizZipUtils.getBinaryContent(url, callback);
}

export const App = class App extends React.Component {
  render() {
    const generateDocument = () => {
      loadFile('https://docxtemplater.com/tag-example.docx', function(
        error,
        content
      ) {
        if (error) {
          throw error;
        }
        const zip = new PizZip(content);
        const doc = new Docxtemplater(zip, {
          paragraphLoop: true,
          linebreaks: true
        });
        doc.setData({
          first_name: 'John',
          last_name: 'Doe',
          phone: '0652455478',
          description: 'New Website'
        });
        try {
          // render the document (replace all occurences of {first_name} by John, {last_name} by Doe, ...)
          doc.render();
        } catch (error) {
          // The error thrown here contains additional information when logged with JSON.stringify (it contains a properties object containing all suberrors).
          function replaceErrors(key, value) {
            if (value instanceof Error) {
              return Object.getOwnPropertyNames(value).reduce(function(
                error,
                key
              ) {
                error[key] = value[key];
                return error;
              },
              {});
            }
            return value;
          }
          console.log(JSON.stringify({ error: error }, replaceErrors));

          if (error.properties && error.properties.errors instanceof Array) {
            const errorMessages = error.properties.errors
              .map(function(error) {
                return error.properties.explanation;
              })
              .join('\n');
            console.log('errorMessages', errorMessages);
            // errorMessages is a humanly readable message looking like this:
            // 'The tag beginning with "foobar" is unopened'
          }
          throw error;
        }
        const out = doc.getZip().generate({
          type: 'blob',
          mimeType:
            'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
        }); //Output the document using Data-URI
        saveAs(out, 'output.docx');
      });
    };

    return (
      <div className="p-2">
        <button onClick={generateDocument}>Generate document</button>
      </div>
    );
  }
};

Docxtemplater in an angular project

There is an online angular demo available on stackblitz.

If you are using an angular version that supports the import keyword, you can use the following code:

import { Component } from "@angular/core";
import Docxtemplater from "docxtemplater";
import PizZip from "pizzip";
import PizZipUtils from "pizzip/utils/index.js";
import { saveAs } from "file-saver";

function loadFile(url, callback) {
  PizZipUtils.getBinaryContent(url, callback);
}

@Component({
  selector: "app-product-list",
  templateUrl: "./product-list.component.html",
  styleUrls: ["./product-list.component.css"]
})
export class ProductListComponent {
  generate() {
    loadFile("https://docxtemplater.com/tag-example.docx", function(
      error,
      content
    ) {
      if (error) {
        throw error;
      }
      const zip = new PizZip(content);
      const doc = new Docxtemplater(zip, { paragraphLoop: true, linebreaks: true });
      doc.setData({
        first_name: "John",
        last_name: "Doe",
        phone: "0652455478",
        description: "New Website"
      });
      try {
        // render the document (replace all occurences of {first_name} by John, {last_name} by Doe, ...)
        doc.render();
      } catch (error) {
        // The error thrown here contains additional information when logged with JSON.stringify (it contains a properties object containing all suberrors).
        function replaceErrors(key, value) {
          if (value instanceof Error) {
            return Object.getOwnPropertyNames(value).reduce(function(
              error,
              key
            ) {
              error[key] = value[key];
              return error;
            },
            {});
          }
          return value;
        }
        console.log(JSON.stringify({ error: error }, replaceErrors));

        if (error.properties && error.properties.errors instanceof Array) {
          const errorMessages = error.properties.errors
            .map(function(error) {
              return error.properties.explanation;
            })
            .join("\n");
          console.log("errorMessages", errorMessages);
          // errorMessages is a humanly readable message looking like this:
          // 'The tag beginning with "foobar" is unopened'
        }
        throw error;
      }
      const out = doc.getZip().generate({
        type: "blob",
        mimeType:
          "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
      });
      // Output the document using Data-URI
      saveAs(out, "output.docx");
    });
  }
}

Docxtemplater in a Vuejs project

There is an online vuejs demo available on stackblitz.

If you are using vuejs 2.0 version that supports the import keyword, you can use the following code:

import Docxtemplater from "docxtemplater";
import PizZip from "pizzip";
import PizZipUtils from "pizzip/utils/index.js";
import { saveAs } from "file-saver";

function loadFile(url, callback) {
  PizZipUtils.getBinaryContent(url, callback);
}

export default {
  methods: {
    renderDoc() {
      loadFile("https://docxtemplater.com/tag-example.docx", function(
        error,
        content
      ) {
        if (error) {
          throw error;
        }
        const zip = new PizZip(content);
        const doc = new Docxtemplater(zip, { paragraphLoop: true, linebreaks: true });
        doc.setData({
          first_name: "John",
          last_name: "Doe",
          phone: "0652455478",
          description: "New Website"
        });
        try {
          // render the document (replace all occurences of {first_name} by John, {last_name} by Doe, ...)
          doc.render();
        } catch (error) {
          // The error thrown here contains additional information when logged with JSON.stringify (it contains a properties object containing all suberrors).
          function replaceErrors(key, value) {
            if (value instanceof Error) {
              return Object.getOwnPropertyNames(value).reduce(function(
                error,
                key
              ) {
                error[key] = value[key];
                return error;
              },
              {});
            }
            return value;
          }
          console.log(JSON.stringify({ error: error }, replaceErrors));

          if (error.properties && error.properties.errors instanceof Array) {
            const errorMessages = error.properties.errors
              .map(function(error) {
                return error.properties.explanation;
              })
              .join("\n");
            console.log("errorMessages", errorMessages);
            // errorMessages is a humanly readable message looking like this:
            // 'The tag beginning with "foobar" is unopened'
          }
          throw error;
        }
        const out = doc.getZip().generate({
          type: "blob",
          mimeType:
            "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
        });
        // Output the document using Data-URI
        saveAs(out, "output.docx");
      });
    }
  },

  template: `
    <button @click="renderDoc">
       Render docx template
    </button>
  `
};

Docxtemplater in a Next.js project

There is an online nextjs demo available on codesandbox.

You can use the following code:

import SiteLayout from "../components/SiteLayout";
import React from "react";
import Docxtemplater from "docxtemplater";
import PizZip from "pizzip";
import { saveAs } from "file-saver";
let PizZipUtils = null;
if (typeof window !== "undefined") {
  import("pizzip/utils/index.js").then(function (r) {
    PizZipUtils = r;
  });
}

function loadFile(url, callback) {
  PizZipUtils.getBinaryContent(url, callback);
}

const generateDocument = () => {
  loadFile("https://docxtemplater.com/tag-example.docx", function (
    error,
    content
  ) {
    if (error) {
      throw error;
    }
    const zip = new PizZip(content);
    const doc = new Docxtemplater().loadZip(zip);
    doc.setData({
      first_name: "John",
      last_name: "Doe",
      phone: "0652455478",
      description: "New Website"
    });
    try {
      // render the document (replace all occurences of {first_name} by John, {last_name} by Doe, ...)
      doc.render();
    } catch (error) {
      // The error thrown here contains additional information when logged with JSON.stringify (it contains a properties object containing all suberrors).
      function replaceErrors(key, value) {
        if (value instanceof Error) {
          return Object.getOwnPropertyNames(value).reduce(function (
            error,
            key
          ) {
            error[key] = value[key];
            return error;
          },
          {});
        }
        return value;
      }
      console.log(JSON.stringify({ error: error }, replaceErrors));

      if (error.properties && error.properties.errors instanceof Array) {
        const errorMessages = error.properties.errors
          .map(function (error) {
            return error.properties.explanation;
          })
          .join("\n");
        console.log("errorMessages", errorMessages);
        // errorMessages is a humanly readable message looking like this:
        // 'The tag beginning with "foobar" is unopened'
      }
      throw error;
    }
    const out = doc.getZip().generate({
      type: "blob",
      mimeType:
        "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
    });
    // Output the document using Data-URI
    saveAs(out, "output.docx");
  });
};

const Index = () => (
  <SiteLayout>
    <div className="mt-8 max-w-xl mx-auto px-8">
      <h1 className="text-center">
        <span className="block text-xl text-gray-600 leading-tight">
          Welcome to this
        </span>
        <span className="block text-5xl font-bold leading-none">
          Awesome Website
        </span>
      </h1>
      <div className="mt-12 text-center">
        <button
          onClick={generateDocument}
          className="inline-block bg-gray-900 hover:bg-gray-800 text-white font-medium rounded-lg px-6 py-4 leading-tight"
        >
          Generate document
        </button>
      </div>
    </div>
  </SiteLayout>
);

export default Index;

Getting access to page number / total number of pages or regenerate Table of Contents

Sometimes, you would like to know what are the total number of pages in the document, or what is the page number at the current tag position.

This is something that will never be achievable with docxtemplater, because docxtemplater is only a templating engine: it does know how to parse the docx format. However, it has no idea on how the docx is rendered at the end: the width, height of each paragraph determines the number of pages in a document.

Since docxtemplater does not know how to render a docx document, (which determines the page numbers), this is why it is impossible to regenerate the page numbers within docxtemplater.

Also, even across different “official” rendering engines, the page numbers may vary. Depending on whether you open a document with Office Online, Word 2013 or Word 2016 or the Mac versions of Word, you can have some slight differences that will, at the end, influence the number of pages or the position of some elements within a page.

The amount of work to write a good rendering engine would be very huge (a few years at least for a team of 5-10 people).

Special character keys with angular parser throws error

The error that you could see is this, when using the tag {être}.

Error: [$parse:lexerr] Lexer Error: Unexpected next character  at columns 0-0 [ê] in expression [être].

This is because angular-expressions does not allow non-ascii characters. You will need angular-expressions version 1.1.0 which adds the isIdentifierStart and isIdentifierContinue properties.

You can fix this issue by adding the characters that you would like to support, for example:

function validChars(ch) {
    return (
        (ch >= "a" && ch <= "z") ||
        (ch >= "A" && ch <= "Z") ||
        ch === "_" ||
        ch === "$" ||
        "ÀÈÌÒÙàèìòùÁÉÍÓÚáéíóúÂÊÎÔÛâêîôûÃÑÕãñõÄËÏÖÜŸäëïöüÿß".indexOf(ch) !== -1
    );
}
function angularParser(tag) {
    if (tag === '.') {
        return {
            get: function(s){ return s;}
        };
    }
    const expr = expressions.compile(
        tag.replace(/(’|‘)/g, "'").replace(/(“|”)/g, '"'),
        {
            isIdentifierStart: validChars,
            isIdentifierContinue: validChars
        }
    );
    return {
        get: function(scope, context) {
            let obj = {};
            const scopeList = context.scopeList;
            const num = context.num;
            for (let i = 0, len = num + 1; i < len; i++) {
                obj = assign(obj, scopeList[i]);
            }
            return expr(scope, obj);
        }
    };
}
new Docxtemplater(zip, {parser:angularParser});

Remove proofstate tag

The proofstate tag in a document marks the document as spell-checked when last saved. After rendering a document with docxtemplater, some spelling errors might have been introduced by the addition of text. The proofstate tag is by default, not removed.

To remove it, one could do the following, starting with docxtemplater 3.17.2

const proofstateModule = require("docxtemplater/js/proof-state-module.js");
doc = new Docxtemplater(zip, {modules: [proofstateModule] });

Adding page break except for last item in loop

It is possible, in a condition, to have some specific behavior for the last item in the loop using a custom parser. You can read more about how custom parsers work here.

It will allow you to add a page break at the end of each loop, except for the last item in the loop.

The template will look like this:

{#users}
The user {name} is aged {age}
{description}
Some other content
{@$pageBreakExceptLast}
{/}

And each user block will be followed by a pagebreak, except the last user.

function angularParser(tag) {
    if (tag === '.') {
        return {
            get: function(s){ return s;}
        };
    }
    const expr = expressions.compile(
        tag.replace(/(’|‘)/g, "'").replace(/(“|”)/g, '"')
    );
    return {
        get: function(scope, context) {
            let obj = {};
            const scopeList = context.scopeList;
            const num = context.num;
            for (let i = 0, len = num + 1; i < len; i++) {
                obj = assign(obj, scopeList[i]);
            }
            return expr(scope, obj);
        }
    };
}
function parser(tag) {
    // We write an exception to handle the tag "$pageBreakExceptLast"
    if (tag === "$pageBreakExceptLast") {
        return {
            get(scope, context) {
                const totalLength = context.scopePathLength[context.scopePathLength.length - 1];
                const index = context.scopePathItem[context.scopePathItem.length - 1];
                const isLast = index === totalLength - 1;
                if (!isLast) {
                    return '<w:p><w:r><w:br w:type="page"/></w:r></w:p>';
                }
                else {
                    return '';
                }
            }
        }
    }
    // We use the angularParser as the default fallback
    // If you don't wish to use the angularParser,
    // you can use the default parser as documented here:
    // https://docxtemplater.readthedocs.io/en/latest/configuration.html#default-parser
    return angularParser(tag);
}
const doc = new Docxtemplater(zip, {parser: parser});
doc.render();

Encrypting files

Docxtemplater itself does not handle the Encryption of the docx files.

There seem to be two solutions for this:

Assignment expression in template

By using the angular expressions options, it is possible to add assignment expressions (for example {full_name = first_name + last_name} in your template. See following part of the doc.

Changing the end-user syntax

If you find that the loop syntax is a bit too complex, you can change it to something more human friendly (but more verbose). This could be used to have a syntax more similar to what the software “HotDocs” provides.

For example, you could be willing to write loops like this :

{FOR users}
Hello {name}
{ENDFOR}

Instead of

{#users}
Hello {name}
{/}

This can be done by changing the prefix of the loop module, which is a builtin module.

const doc = new Docxtemplater(zip);
doc.modules.forEach(function (module) {
    if (module.name === "LoopModule") {
        module.prefix.start = "FOR "
        module.prefix.start = "ENDFOR "
    }
});

Note that if you don’t like the default delimiters which are { and }, you can also change them, for example :

If you prefer to write :

[[FOR users]]
Hello [[name]]
[[ENDFOR]]

You could write your code like this :

const doc = new Docxtemplater(zip, { delimiters: { start: "[[", end: "]]" } });
doc.modules.forEach(function (module) {
    if (module.name === "LoopModule") {
        module.prefix.start = "FOR "
        module.prefix.start = "ENDFOR "
    }
});

Note that it is however not possible to use no delimiters at all, docxtemplater forces you to have some delimiters.

Similarly, for each paid module (image module, …), you can set your own prefixes as well.

For example, for the image module, if you would like to write {IMG mydata} instead of {%mydata} and {CENTERIMG mydata} instead of {%%mydata}, you can write your code like this :

const ImageModule = require("docxtemplater-image-module");

const opts = {};
opts.centered = false;
opts.getImage = function (tagValue, tagName) {
  return fs.readFileSync(tagValue);
};

opts.getSize = function (img, tagValue, tagName) {
  return [150, 150];
};

const imageModule = new ImageModule(opts);
imageModule.prefix.normal = "IMG "
imageModule.prefix.centered = "CENTERIMG "
const doc = new Docxtemplater(zip, { modules: [imageModule], delimiters: { start: "[[", end: "]]" } });
doc.modules.forEach(function (module) {
    if (module.name === "LoopModule") {
        module.prefix.start = "FOR "
        module.prefix.start = "ENDFOR "
    }
});