2

I'm using https://github.com/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.

Joe
  • 4,274
  • 32
  • 95
  • 175

2 Answers2

6

By default, you have to do : {#address}{city}{/address}

if you use the angularParser, it would be {address.city} : http://docxtemplater.readthedocs.io/en/latest/configuration.html?highlight=angular#custom-parser

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

edi9999
  • 19,701
  • 13
  • 88
  • 127
0

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

Amina Darwish
  • 328
  • 3
  • 5