guigrpa / docx-templates

Template-based docx report creation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature request: Field Codes as alternative to cmdDelimiter characters

Amerlander opened this issue · comments

I'm searching for ways to improve the ease of use when creating templates.

There is a function in word called Field Codes (STRG+F9 to insert one). I was thinking if it would be possible to use these instead of delimiting characters. Has anyone already thought about this, or are there technical limitations I don't have in mind?

Advantages of Field Codes:

  • Easy to insert
  • Visual highlighting of fields
  • Get deleted as whole field when deleted by backspace
  • Brackets are bolder than regular brackets, so it's better to see them and differentiate between brackets inside the JS
  • No spellchecking inside fields

The XML for {ABC} is:

<w:r>
    <w:rPr>
        <w:lang w:val="en-GB"/>
    </w:rPr>
    <w:fldChar w:fldCharType="begin"/>
</w:r>
<w:r>
    <w:rPr>
        <w:lang w:val="en-GB"/>
    </w:rPr>
    <w:instrText xml:space="preserve">
        ABC
    </w:instrText>
</w:r>
<w:r>
    <w:rPr>
        <w:lang w:val="en-GB"/>
    </w:rPr>
    <w:fldChar w:fldCharType="end"/>
</w:r>

Here are two pictures of a regular test template and one with field codes. The cursor is in both inside the “Work Title” field:
image
image

Just found out they disapear as soons as the documents is reopened. Thats intereseting, becouse they are in the XML.
But it does not make sense to create templates that cant be edited later I guess.

image