We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Insert Conditional IF Blocks inside Foreach Loop Region in Word Document & Build Reports using C# .NET | Mustache Syntax

I’m using the mail merge mustache templating functionality to populate a document (https://docs.aspose.com/words/net/mail-merge-template-from-mustache-syntax/). I’ve been using the #foreach syntax for looping regions, I wondered if there was an equivalent for if blocks?

The link above shows an inline if syntax, like:

{ IF “{{ GENDER }}” = “MALE” “true text” “false text” }

but I’m really looking for a region based syntax as I may need to include more complicated content such as lists, tables, images etc. Ideally something like the following where I can provide a truthy value and have it shown/hidden accordingly:

{{#if is_male}} Some more complicated content in here {{/if is_male}}

@jrwal200ok,

Please see these sample template and output Word documents (Use Of if else Mustache Syntax.zip (18.8 KB)) and try running the following C# code:

Document doc = new Document("C:\\temp\\UseOfifelseMustacheSyntax\\Use Of if else Mustache Syntax.docx");
doc.MailMerge.UseNonMergeFields = true;
doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveContainingFields;
doc.MailMerge.Execute(new string[] { "GENDER" }, new string[] { "MALE" });
doc.Save("C:\\temp\\UseOfifelseMustacheSyntax\\20.10.docx");

Thanks, yes I’ve seen that syntax, but it’s too limited for my use case. What I’m looking for is some region-based syntax where I can wrap a start/end if tag around a larger chunk of a document (in much the same way that the #foreach functionality works).

I wouldn’t require any additional logic (the provided sample uses equality operators etc for example, I’d only ever be passing in a single true/false value) in case that simplified things at all

@jrwal200ok,

Another way is to make use of ‘Conditional Blocks’ of LINQ Reporting Engine. Please refer to the following pages: