文档模板。嵌套对象

Docxtemplater. Nested objects

我正在使用 https://github.com/open-xml-templating/docxtemplater

我的数据:

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

如何使用此嵌套对象在 docx 中编写语法?

这不起作用:

{address.city}

在文档中找不到任何示例。

默认情况下,您必须执行:{#address}{city}{/address}

如果你使用 angularParser,它将是 {address.city} : http://docxtemplater.readthedocs.io/en/latest/configuration.html?highlight=angular#custom-parser

https://github.com/open-xml-templating/docxtemplater/issues/243

使用angularParser

https://docxtemplater.readthedocs.io/en/latest/angular_parse.html

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


          function angularParser(tag) {
            if (tag === ".") {
              return {
                get: function (s) {
                  return s;
                }
              };
            }
            const expr = expressions.compile(
              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.commaNum = 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
          });