open-xml-templating / docxtemplater

Generate docx, pptx, and xlsx from templates (Word, Powerpoint and Excel documents), from Node.js or the browser. Demo: https://www.docxtemplater.com/demo. #docx #office #generator #templating #report #json #generate #generation #template #create #pptx #docx #xlsx #react #vuejs #angularjs #browser #typescript #image #html #table #chart
https://www.docxtemplater.com
Other
3.07k stars 350 forks source link

Cannot resolve @Name placeholder #728

Closed dtslvr closed 1 year ago

dtslvr commented 1 year ago

Environment

How to reproduce my problem

My template is the following:

Dear @Name

With the following Typescript code:

import * as PizZip from 'pizzip';

const Docxtemplater = require('docxtemplater');

...

const content = fs.readFileSync(filePath, 'binary');

const zip = new PizZip(content);

const doc = new Docxtemplater(zip, {
  delimiters: { start: '@', end: ' ' }
});

doc.render({
  'Name': 'John'
});

...

I would expect it to be

Dear John

but I get the following error:

{
  "error": [
    {
      "name": "TemplateError",
      "message": "Unopened tag",
      "stack": "Error: Unopened tag\n    at new XTTemplateError (/Users/user/Documents/dev/application/build/server.js:173875:16)\n    at getUnopenedTagException (/Users/user/Documents/dev/application/build/server.js:173925:13)\n    at /Users/user/Documents/dev/application/build/server.js:174642:23\n    at Array.forEach (<anonymous>)\n    at getDelimiterErrors (/Users/user/Documents/dev/application/build/server.js:174617:20)\n    at parseDelimiters (/Users/user/Documents/dev/application/build/server.js:174777:16)\n    at Object.parse (/Users/user/Documents/dev/application/build/server.js:174865:28)\n    at XmlTemplater.preparse (/Users/user/Documents/dev/application/build/server.js:177541:32)\n    at Docxtemplater.precompileFile (/Users/user/Documents/dev/application/build/server.js:173495:19)\n    at /Users/user/Documents/dev/application/build/server.js:173592:18",
      "properties": {
        "xtag": "John",
        "id": "unopened_tag",
        "context": "John",
        "offset": 4,
        "explanation": "The tag beginning with \"John\" is unopened",
        "file": "docProps/core.xml"
      }
    }
    ...
  ]
}

Additional info

Possibly related to #726.

edi9999 commented 1 year ago

Yes, from what I see, this would be the same issue.

I hope to be working on it next week and provide a fix then.

The issue is that since " " is now your end tag, any space in the document that doesn't have a matching startTag (a "@" symbol) will throw an error.

dtslvr commented 1 year ago

Thank you very much for your prompt response, Edgar.

As this seems to be a duplicate of #726 and I will most likely use a different approach, I am closing this issue.