Word Document Template - Range Tag Bug with empty collection

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

    • Post Options:
    • Link

    Posted 18 December 2023, 11: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 20 December 2023, 6:04 am 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 20 December 2023, 7:46 am EST

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

  • Posted 21 December 2023, 8:15 am 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