You are currently offline, serving cached version

This module exposes two tags to change subsection :

  • The tag {:subsection doc} will create a new subsection on the location of the tag (and use the header and footer that are present in the document doc).
  • The tag {:replacesection doc} will replace the current subsection (and use the header and footer that are present in the document doc).

Installation

You will need docxtemplater v3: npm install docxtemplater

Install this module with npm install --save "$url"

Usage

Your docx should contain the text: {:subsection subdoc} or `{:replacesection subdoc}.

After installing the module, you can use a working demo by running node sample.js.

At the place where you put the {:subsection subdoc} tag, a new page will be created and that page will have as headers and footers the same headers and footers as those found in the subdocument

Subsection Rendering

You can render the values in your subsection by using a custom parser and render the document with the current scope.

For example :

const SubSectionModule = require("docxtemplater-subsection-module");
const subsectionModule = new SubSectionModule({});
const expressions = require("angular-expressions");
const doc = new Docxtemplater(zip, {
    modules: [subsectionModule],
    parser: function recursiveParser(tagString) {
        return {
            get(scope, data) {
                const module =
                    data &&
                    data.meta &&
                    data.meta.part &&
                    data.meta.part.module;
                const isIncludeTag =
                    [
                        "pro-xml-templating/subsection-module",
                        "pro-xml-templating/replacesection-module",
                    ].indexOf(module) !== -1;
                let value = null;
                if (tagString === ".") {
                    value = scope;
                } else {
                    value =
                        expressions.compile(tagString)(scope);
                }
                if (isIncludeTag && value) {
                    value.setData(scope);
                    value.render();
                }
                return value;
            },
        };
    },
});

CHANGELOG

3.3.12

Use @xmldom/xmldom instead of xmldom, see this github issue

3.3.11

Generate files in built with correct filename In previous versions, the filename was always build/docxtemplater.js. Now the filename is error-location-module.js The .min.js file is also created now.

3.3.10

Add typescript definitions for public API

3.3.9

Move webpack from dependency to devDependency (which triggered an error during installation on node version 10)

3.3.8

Internal change to allow to match tags with non-breaking space.

When entering Ctrl+Shift+Space, a "non-breaking" space is created.

When using a non-breaking space in the tag {:subsection doc}, the tag would not be replaced correctly.

3.3.7

Calculate maxDocPrId lazily (only at the time when we need the value), to avoid overhead when attaching the module.

3.3.6

Declare supportedFileTypes, which allows to use this module with the new docxtemplater constructor which was introduced in docxtemplater 3.17.

3.3.5

Bugfix footer appearing multiple times in output

3.3.4

Bugfix footer not correctly included when the XML of the main document is not formatted the exact way xmldom wants it.

3.3.3

Bugfix when including images that are outside /word/media or have absolute paths

3.3.2

Bugfix when including footer or header with links (URLS). The error message was : Cannot read property 'asBinary' of null

3.3.1

Multiple bugfixes :

  • Bugfix for including footer in separate documents when the header also contains footers.

  • Bugfix when rId are not numeric

3.3.0

Add support for including headers/footers from separate documents.

3.2.4

  • Update browser build to use XMLSerializer instead of xmldom

  • Use requiredAPIVersion

3.2.3

  • Move docxtemplater from devDependencies to dependencies

Explanation : On some versions of npm (notably 5.8.0), when having a package containing docxtemplater-subsection-module, the installation will generate a tree of node_modules that puts the module on a level where it has no access to docxtemplater. By explicitly asking it as a dependency, this issue is avoided.

3.2.2

  • Make module compatible with docxtemplater version 3.5 and below.

Explanation : Recently, the scopemananger API (internal API) has changed, this new version of the footnotes module makes the module work with both versions newer than 3.6 and older than 3.6 of docxtemplater.

3.2.1

Make it possible to include the first section of a document having multiple sections.

3.2.0

  • Add support for being included by subtemplate module

  • Fix corruption for replacesection tag

3.1.1

Add meta context argument to custom parser with information about the tag for each types of tags

3.1.0

Add {:replacesection} tag that allows to replace a section instead of adding it.

3.0.3

Fix issue with multiple subsection tag in same document

3.0.2

Fix corruption with loop when the headers contain objects such as arrows

3.0.1

Fix corrupted document when the tag {:subsection} is not used in the document

3.0.0

Initial release