Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IsNullOrEmpty(string) and List.Count > 0 in mustache-sharp

I use mustache-sharp as template engine

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 ?

like image 316
HamedFathi Avatar asked Feb 09 '26 21:02

HamedFathi


1 Answers

You can try to create a custom ContentTagDefinition and register it in HtmlFormatCompiler.

For example:

  1. IsNullOrEmpty

    public class IsNullOrEmptyTagDefinition : ContentTagDefinition
    {
        private const string conditionParameter = "condition";
    
        public IsNullOrEmptyTagDefinition()
            : base("IsNullOrEmpty")
        {}
    
        public override IEnumerable<TagParameter> GetChildContextParameters()
        {
            return new TagParameter[0];
        }
    
        public override bool ShouldGeneratePrimaryGroup(Dictionary<string, object> arguments)
        {
            object condition = arguments[conditionParameter];
            return isConditionSatisfied(condition);
        }
    
        protected override IEnumerable<TagParameter> GetParameters()
        {
            return new TagParameter[] { new TagParameter(conditionParameter) { IsRequired = true } };
        }
    
        protected override bool GetIsContextSensitive()
        {
            return false;
        }
    
        private bool isConditionSatisfied(object condition)
        {
            if (condition == null)
            {
                return true;
            }
    
            return condition is string ? string.IsNullOrEmpty(condition as string) : false;
        }
    
    }
    
  2. Any

    public class AnyTagDefinition : ContentTagDefinition
    {
        private const string conditionParameter = "condition";
    
        public AnyTagDefinition()
            : base("Any")
        {}
    
        public override IEnumerable<TagParameter> GetChildContextParameters()
        {
            return new TagParameter[0];
        }
    
        public override bool ShouldGeneratePrimaryGroup(Dictionary<string, object> arguments)
        {
            object condition = arguments[conditionParameter];
            return isConditionSatisfied(condition);
        }
    
        protected override IEnumerable<TagParameter> GetParameters()
        {
            return new TagParameter[] { new TagParameter(conditionParameter) { IsRequired = true } };
        }
    
        protected override bool GetIsContextSensitive()
        {
            return false;
        }
    
        private bool isConditionSatisfied(object condition)
        {
            if (condition is IEnumerable)
            {
                return (condition as IEnumerable).Cast<object>().Any();
            }
    
            return false;
        }
    
    }
    
  3. Register both tags

    HtmlFormatCompiler compiler = new HtmlFormatCompiler();
    compiler.RegisterTag(new IsNullOrEmptyTagDefinition(), true);
    compiler.RegisterTag(new AnyTagDefinition(), true);
    
like image 80
PiKos Avatar answered Feb 16 '26 22:02

PiKos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!