2

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

我的数据:

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

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

这不起作用:

{address.city}

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

4

2 回答 2

4

默认情况下,您必须这样做:{#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

于 2016-08-28T13:51:55.357 回答
0

使用角度解析器

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
          });

于 2020-10-22T13:14:57.297 回答