DotLiquid/Liquid access to dictionary

后端 未结 2 882
时光说笑
时光说笑 2021-02-10 00:06

I am using DotLiquid template engine and trying access dictionary value in template. I have passed to template this drop:

public class SomeDrop : Drop
{
   publi         


        
2条回答
  •  有刺的猬
    2021-02-10 00:43

    You need to set Template.NamingConvention before creating any drop objects. For performance reasons, the base Drop constructor caches all public instance members using the current naming convention. Even if you then change the naming convention, those cached properties are not reset.

    This code works for me:

    public class SomeDrop : Drop
    {
        public Dictionary MyDictionary { get; set; }
    }
    
    [Test]
    public void StackOverflow()
    {
        Template.NamingConvention = new CSharpNamingConvention();
        const string template = "{{ this.MyDictionary.myKey }}";
    
        var someDropInstance = new SomeDrop
        {
            MyDictionary = new Dictionary { { "myKey", 1 } }
        };
    
        var preparedTemplate = Template.Parse(template);
        Assert.That(
            preparedTemplate.Render(Hash.FromAnonymousObject(new { @this = someDropInstance })),
            Is.EqualTo("1"));
    }
    

    I admit this is a bit of a gotcha - this is not the first time this issue has been raised. I haven't yet come up with a satisfactory solution, but any suggestions are welcome.

提交回复
热议问题