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

add ability to work with loop-index in FOR-Loops

Open devtim123 opened this issue 3 years ago • 2 comments

Hey Guillermo, i needed to add the possibility to use the index of a FOR-loop in my templates, e.g. to display different stuff for % modulo. For using this you just have to add the suffix "Index" to your defined variable. I also edit the readme for using this.

maybe you find this also helpful.

Beste regards, tim

devtim123 avatar Nov 05 '20 16:11 devtim123

Coverage Status

Coverage increased (+0.009%) to 92.388% when pulling a04e5cf7174bb0851bf97e259fd3b34c12576bcc on devtim123:master into d0d21eea7a7c8720c2778c9e4e5f2180fec8e1a3 on guigrpa:master.

coveralls avatar Nov 05 '20 17:11 coveralls

i also altered the way new lines get inserted into the doc, solving the issue https://github.com/guigrpa/docx-templates/issues/143

solved by inspecting the diff from rendered document.xml with docx-template and the word-docx version. it looks like you cant add <w:br/> tags into <w:t xml:space="preserve">, wrong example:

<w:r>
   <w:t xml:space="preserve">
      You need to sit down and sketch more fucking ideas
      <w:br/>
      because stalking your ex on facebook isn’t going to get you anywhere.
   </w:t>
</w:r>

It has to be inside <w:r> tag, like this correct example:

<w:r>
   <w:t xml:space="preserve">You need to sit down and sketch more fucking ideas</w:t>
</w:r>
<w:r>
   <w:br/>
   <w:t xml:space="preserve">because stalking your ex on facebook isn’t going to get you anywhere.</w:t>
</w:r>

devtim123 avatar Nov 05 '20 19:11 devtim123

This PR is a bit before my time, but this is great work. It turns out that we had something like this under the hood all along, so I just merged that instead, see PR #323 . Thanks a lot for your effort, though, and sorry it took so long to reply to this.

jjhbw avatar Aug 16 '23 07:08 jjhbw

@jjhbw Something like this would be handy for referencing nested loops indexes (e.g. $personIdx to align with current $idx naming), and that'd solve the limitation of just having $idx. The existing $idx should be kept as well so an editor doesn't need to be explict - which helps with simple loops, moving code around in a document, or when the loop variable name get changed etc.

davidjb avatar Aug 16 '23 23:08 davidjb

@davidjb makes sense. Can you open a feature request issue to further discuss that? We can probably build on top of this PR.

jjhbw avatar Aug 17 '23 06:08 jjhbw