DotLiquid-介绍

简介

DotLiquid是流行的Liquid模板引擎的.NET实现。
官网:http://dotliquidmarkup.org/
语法:https://shopify.github.io/liquid/
源码:https://github.com/dotliquid/dotliquid

简单案例

Template template = Template.Parse("hi {{name}}");  // 解析和编译模板
template.Render(Hash.FromAnonymousObject(new { name = "tobi" })); // 输出 => "hi tobi"

暴露对象

DotLiquid默认情况下只接受有限类型作为Render方法的参数,包括.NET原始类型(int,float,string等),和一些集合类型,包含IDictionary,IList和IIndexable(自定义DotLiquid接口)。
如果它支持任意类型,则可能导致属性或方法无意间暴露给模板开发者。DotLiquid旨在让用户创作和上传自己的模板。因此,该库非常注重确保不会通过生成的HTML意外泄露任何信息。开发人员必须在模板中引用它们之前,将所有类的每个属性明确标记为可以暴露的安全对象。
可以有如下方法暴露对象:

方法1:ViewModel继承Drop

public class Person : Drop
    {
        public string Name { get; set; }
        public override object BeforeMethod(string method)
        {//如果未找到属性或方法,调用此方法
            return "默认值";
        }
    }

方法2:标记LiquidType特性

[LiquidType("*")]//or [LiquidType("Name")]
    public class Person1
    {
        public string Name { get; set; }
    }

方法3:使用RegisterSafeType方法注册(推荐)

Template.RegisterSafeType(typeof(Show),new[] { "Name", "Cast" });

如果每个类都使用以上方法太繁琐,可以通过反射简化对象暴露操作,提供一个助手类:

public class SafeTypeHelper
    {
        public static void RegisterViewModel(Type rootType)
        {
            rootType
               .Assembly
               .GetTypes()
               .Where(t => t.Namespace == rootType.Namespace)
               .ToList()
               .ForEach(RegisterSafeTypeWithAllProperties);
        }

        public static void RegisterSafeTypeWithAllProperties(Type type)
        {
            Template.RegisterSafeType(type,
               type
               .GetProperties()
               .Select(p => p.Name)
               .ToArray(), o => o.ToString());
        }
    }

命名约定

默认使用RubyNamingConvention,也可以修改为c#的命名约定Template.NamingConvention = new CSharpNamingConvention();

Template.NamingConvention = new RubyNamingConvention();
Template template = Template.Parse("{{ person.name }},{{ person.pet_name }}");
string html = template.Render(Hash.FromAnonymousObject(new { person = new Person { Name = "fan", PetName = "ahuang" } }));

过滤器

DotLiquid内置了一些过滤器,我们也可以创建自己的过滤器,过滤器就是一个方法:

注册过滤器,有两种方式:

1、全局注册

Template.RegisterFilter(typeof(MyFilter));

2、Render方法传参

Template.Parse(" {{ 'Typo' | link_to: 'http://typo.leetsoft.com' }} ").Render(new RenderParameters(CultureInfo.InvariantCulture) { LocalVariables = _assigns, Filters = new[] { typeof(FunnyFilter) } })

标签

posted @ 2021-03-13 23:51  .Neterr  阅读(1792)  评论(0编辑  收藏  举报