docx-templates icon indicating copy to clipboard operation
docx-templates copied to clipboard

Special characters like in docx template

Open JonRC opened this issue 2 years ago • 3 comments

  • When I use a docx with a ? character as template, it throws an error.

Erro: Error executing command 'name?': SyntaxError: Unexpected end of input

DOCX file: "your name is {name?}"

image

CODE:

createReport({template, data: {name: "Jonathan"}})
  • running on NodeJs 16.16.0 - Ubuntu

How can I use ? and other special characters in a docx template?

JonRC avatar Aug 01 '22 20:08 JonRC

Why do you want a ?.

name? is not valid JavaScript so the error is expected...

mathe42 avatar Aug 01 '22 20:08 mathe42

Because users will create the templates. They need to write a question to be replaced. For example, I have a user that wants to use {what is your name?}

I'm wondering if I can disable the javascript feature and just use the replament feature.

JonRC avatar Aug 01 '22 21:08 JonRC

That is not possible. All between {...} has to be valid JS.

I never had user-provided templates. But you can create a list of valid fields where you can document the complete stuff...

Basicly what you need is a preprocessor-feature where you get the content between {...} and return valid JS code. (would be a hook into it here https://github.com/guigrpa/docx-templates/blob/master/src/processTemplate.ts#L476 )

I strongly sugest not to do this. But if you need it that would be your entry-point into implementing it.

mathe42 avatar Aug 01 '22 21:08 mathe42

Thanks for replying @mathe42 ! Closing this because it seems answered.

jjhbw avatar Aug 02 '22 07:08 jjhbw