c# - IsNullOrEmpty(string) 和 List.Count > 0 in mustache-sharp

标签 c# template-engine mustache

我使用 mustache-sharp作为模板引擎

我想知道到底有没有用这个模板引擎,有两个检查条件

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

谁能指导我如何创建上述标签?

最佳答案

您可以尝试创建一个自定义的 ContentTagDefinition 并在 HtmlFormatCompiler 中注册它。

例如:

  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. 任何

    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. 注册两个标签

    HtmlFormatCompiler compiler = new HtmlFormatCompiler();
    compiler.RegisterTag(new IsNullOrEmptyTagDefinition(), true);
    compiler.RegisterTag(new AnyTagDefinition(), true);
    

关于c# - IsNullOrEmpty(string) 和 List.Count > 0 in mustache-sharp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38079566/

相关文章:

c# - 为什么win32异常没有被c#异常处理机制捕获

c# - P/调用 : converting this signature to Managed c#

java - jooq 从 sql 文件插入

c# - 如何在 C# 的 checkedlistbox 中默认选中两个项目

javascript - Swig (Node.js) 中的 JSON.parse() ?

javascript - jQuery:模板化数据

java - Java 中的安全模板引擎是什么?

javascript - mustache 迭代数组的对象

javascript - 循环遍历 mustache 中的整数,可能吗?

c# - 将 varbinary 转换为 xml C#