ngs-doo / TemplaterExamples

Creating reports in .NET and Java
https://templater.info
The Unlicense
48 stars 27 forks source link

How to use collapseIf plugin multiple times for the same tag? #71

Closed Kobus-Smit closed 5 months ago

Kobus-Smit commented 6 months ago

Hi @zapov

We have a template where we conditionally show certain tables and/or list entries, using a showIf custom plugin. It works in a simple template, but not in a more complicated template where we use showIf for the same tagValue with multiple expression multiple times in the document. It would correctly collapse at some places, but not at other places later in the template etc.

I could reproduce our issue by running the CollapseRegion example with this template: Collapse.docx

I just modified the example's Collapse.docx by

  1. Copying UC check message: [[ucCheckResponse]:collapseIf(Ok)] under Application and pasting it under Applicant twice
  2. Then modify the second one to OK UC check message: [[ucCheckResponse]:collapseIf(Not good enough)]

image

Expected: Collapse-Result-Expected.docx

image

Actual: Collapse-Result-Wrong.docx

image

It does work with a simpler template: Collapse-Minimal-Works.docx

Result: Collapse-Minimal-Works-Result.docx

image

Any ideas how I can get the first template to work by modifying the customIf plugin?

Thanks, Kobus

zapov commented 6 months ago

While there are certain changes you could do with customIf plugin (eg return ThisTag or CurrentContext) the main change is to use a different repeating region. If Templater detects multiple repeating regions it will behave slightly different then when it detects multiple tables. It will process each table on its own, while with this special regions it will try to find common/parent document part for all of those regions.

So one change you need to do is instead of repeating regions use tables without borders. But this only partly solves the problem as it seems in this case you also need to help Templater a bit with :all metadata. In which case it will continue over other tags. I will look if this can be improved in the future not to require this all metadata, but this should get you the behavior you are expecting. Collapse.docx

The best way to understand that is to use debugger and see what happens on each of those resize.

Kobus-Smit commented 6 months ago

Thanks @zapov

I had tried all the Handled. enums in the customIf plugin and it did not work for me.

I modified my Collapse.docx by adding the :all and moving the [[ucCheckResponse... into borderless tables (similar to your attachment) and it works fine.

But adding :all did not help with my larger template and custom plugin. Also not sure where and when I should add :all and if I can just add it to all tags to be safe (though ugly)? (I added :all to your Collapse.docx and it still worked fine there)

My template's tags are already in borderless tables, I toggle View Gridlines to show it: image

For example when debugging Templater-Issue-71.docx

Up to step 18 is correct (for SMB data) image

But then step 19 removes the the wrong table... image

Step 20 removes the correct table.

Kobus-Smit commented 6 months ago

TemplaterPlugin.zip

zapov commented 5 months ago

So it turns out this is due to implementation of this plugin which uses smart low level resize(string[], 0) instead of exact low level resize(tag position[], 0). I will change this example implementation to use the explicit one down the line so reduce future confusion