javascript - Docxtemplater. Nested objects - Stack Overflow

admin2025-04-21  0

I'm using

My data:

var person = {
  name: 'Joe',
  address: {
    city: 'Stockholm',
    postal: 45123
  }
}

How do write the syntax in the docx with this nested object?

This is not working:

{address.city}

Can't find any example in the docs.

I'm using https://github./open-xml-templating/docxtemplater

My data:

var person = {
  name: 'Joe',
  address: {
    city: 'Stockholm',
    postal: 45123
  }
}

How do write the syntax in the docx with this nested object?

This is not working:

{address.city}

Can't find any example in the docs.

Share Improve this question asked Aug 15, 2016 at 14:21 JoeJoe 4,27432 gold badges106 silver badges180 bronze badges 1
  • Use angular parser – Amina Darwish Commented Oct 22, 2020 at 13:17
Add a ment  | 

2 Answers 2

Reset to default 7

There are three possibilities, one with angular-expressions, one by writing a very simple parser yourself, or one without any parser.

With angular expressions

The "angular-expressions" parser gives you the ability to parse more plex expressions :

{address.city}

To use it you also have to install angular-expressions :

npm install --save angular-expressions
const expressionParser = require("docxtemplater/expressions.js");
const doc = new Docxtemplater(zip, { parser: expressionParser });
doc.render(/* data */);

Without angular expressions :

const doc = new Docxtemplater(zip, {
    paragraphLoop: true,
    linebreaks: true,
    parser(tag) {
        const splitted = tag.split(".");
        return {
            get(scope) {
                if (tag === ".") {
                    return scope;
                }
                let s = scope;
                for (
                    let i = 0, len = splitted.length;
                    i < len;
                    i++
                ) {
                    const key = splitted[i];
                    s = s[key];
                }
                return s;
            },
        };
    },
});
doc.render({
    user: {
        name: "John",
    },
});

This allows you to parse {user.name} with nested property access.

Using just builtin features :

If you don't want to add a parser option (which relies on an external dependency), you will have to write in your template :

{#address}{city}{/}

This creates a section that will have as current context the "address" object, and then fetch the "city" key from the address object.

use angularParser

https://docxtemplater./docs/angular-parse/

var expressions = require("angular-expressions");


          function angularParser(tag) {
            if (tag === ".") {
              return {
                get: function (s) {
                  return s;
                }
              };
            }
            const expr = expressions.pile(
              tag.replace(/(’|‘)/g, "'").replace(/(“|”)/g, '"')
            );

            expressions.filters.upper = function (input) {
              // This condition should be used to make sure that if your input is undefined, your output will be undefined as well and will not throw an error
              if (!input) return input;
              return input.toUpperCase();
            };

            expressions.filters.maNum = function (input) {
              // This condition should be used to make sure that if your input is undefined, your output will be undefined as well and will not throw an error
              if (!input) return input;
              if (!isNaN(input))
                return input.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
              else return input;
            };

            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 = merge(obj, scopeList[i]);
                }
                return expr(scope, obj);
              }
            };
          }

          function nullGetter(part, scopeManager) {
            if (!part.module) {
              return " ";
            }
            if (part.module === "rawxml") {
              return "";
            }
            return "";
          }

var doc = new window.docxtemplater().loadZip(zip).setOptions({
            linebreaks: true,
            parser: angularParser,
            nullGetter: nullGetter
          });

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745241738a292050.html

最新回复(0)