Docxtemplater. Вложенные объекты
Я использую https://github.com/open-xml-templating/docxtemplater
Мои данные:
var person = {
name: 'Joe',
address: {
city: 'Stockholm',
postal: 45123
}
}
Как написать синтаксис в DOCX с этим вложенным объектом?
Это не работает:
{address.city}
Не могу найти ни одного примера в документации.
1 ответ
По умолчанию вы должны сделать: {#address}{city}{/address}
если вы используете angularParser, это будет {address.city}: http://docxtemplater.readthedocs.io/en/latest/configuration.html?highlight=angular
См. 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
});