Word Document Template - Range Tag Bug with empty collection

Posted by: info on 18 December 2023, 2:39 am EST

    • Post Options:
    • Link

    Posted 18 December 2023, 2:39 am EST

    If I use a range Tag ae

    {{#ds.Items}}  
           Name: {{ds.Items.Name}}
            I'm should not be here if items is empty
     {{/ds.Items}}

    Then I would expect nothing to be rendered with an empty list.

    I can achieve this desired behavior with ‘hbi-empty’.

    {{#ds.Items}:hbi-empty()}  
           Name: {{ds.Items.Name}}
            I'm should not be here if items is empty
        {{/ds.Items}}

    Unfortunately, ‘hbi-empty’ does not work with a list that contains only one element. With only one element nothing is rendered

    I’ve created a small sample repo for you

    https://github.com/Amberg/MesciusBug/blob/main/Program.cs

  • Posted 19 December 2023, 9:04 pm EST

    Hi,

    You can achieve your requirement by below template:

    @" {{ds.Title}}
                                            {{#ds.Items}}  
                                                Name: {{ds.Items.Name}:hide-block-if-empty()}
                                                I'm should not be here if items is empty
                                            {{/ds.Items}}"

    Please refer the attached sample for the same : DsWordTemplateDemo.zip

    Best Regards,

    Nitin

  • Posted 19 December 2023, 10:46 pm EST

    Ok thanks it works with this template - but the behavior seems strange to me

  • Posted 20 December 2023, 11:15 pm EST

    Hi,

    This is working because of the null value under Items.Name, that’s why the whole block is hidden.

    If you want to achieve this requirement with ds.Items, then you need to set it like:

    @" {{ds.Title}}
                                            {{#ds.Items}}{{ds.Items}:hide-block-if-empty()}  
                                                Name: {{ds.Items.Name}}
                                                I'm should not be here if items is empty
                                            {{/ds.Items}}"

    The block will be hidden if there is no content under ds.Items collection template. You can also refer this for the same: https://developer.mescius.com/document-solutions/dot-net-word-api/docs/online/conditionally-hide-blocks.html#:~:text=as%20shown%20below%3A-,Hide%20Null%20Block,-This%20example%20applies

    Please refer the attached modified sample for the same: DsWordTemplateDemo_Mod.zip

    Best Regards,

    Nitin

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels