jehugaleahsa / mustache-sharp

An extension of the mustache text template engine for .NET.
The Unlicense
306 stars 78 forks source link

IsNullOrEmpty(string) and List.Count > 0 #63

Open HamedFathi opened 8 years ago

HamedFathi commented 8 years ago

I want to know Is there anyway by using this template engine and have two conditions for checking

1) IsNullOrEmpty(string)  => e.g. {{#IsNullOrEmpty MyName}}} {{/IsNullOrEmpty}}
2) List.Count > 0              => e.g. {{#Any Persons}} {{/Any}}

Can anyone guide me How can I create tags like above ?

vfsdan commented 5 years ago

An easy workaround for this is to add a count method to your model

public class model list persons {get; set;} int personCount {get; set;}

Then instead of doing a List.count check, set the personcount to persons.count in the model,

model.personCount = persons.count;

then in your template just check personcount before rendering any list related content

{{#if personCount}}

only show this text if there are list items..

{{#each persons}} ..... {{/each}} {{/if}}