DotLiquid /液体进入词典 [英] DotLiquid/Liquid access to dictionary
问题描述
我使用 DotLiquid 模板引擎和模板尝试访问字典值。
我已经传递到模板此降:
I am using DotLiquid template engine and trying access dictionary value in template. I have passed to template this drop:
public class SomeDrop : Drop
{
public Dictionary<string, object> MyDictionary {get; set;}
}
var someDropInstance = SomeDrop
{
MyDictionary = new Dictionary<string, object> {{"myKey", 1}}
}
Template.NamingConvention = new CSharpNamingConvention();
var preparedTemplate = Template.Parse(template);
var templateOutput = preparedTemplate.Render(Hash.FromAnonymousObject(new { @this = someDropInstance }));
在模板,我不能访问的myKey值
{{} this.MyDictionary.myKey}
既不是
{{this.MyDictionary ['的myKey']}}
In template i can't access to myKey value as
{{ this.MyDictionary.myKey }}
neither as
{{ this.MyDictionary['myKey'] }}
推荐答案
您需要设置 Template.NamingConvention
创建任何之前下降对象。出于性能的考虑,基本挂断
构造的缓存使用当前的命名约定所有的公共实例成员。即使你再改变命名规则,这些缓存的属性不会被重置
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<string, object> MyDictionary { get; set; }
}
[Test]
public void StackOverflow()
{
Template.NamingConvention = new CSharpNamingConvention();
const string template = "{{ this.MyDictionary.myKey }}";
var someDropInstance = new SomeDrop
{
MyDictionary = new Dictionary<string, object> { { "myKey", 1 } }
};
var preparedTemplate = Template.Parse(template);
Assert.That(
preparedTemplate.Render(Hash.FromAnonymousObject(new { @this = someDropInstance })),
Is.EqualTo("1"));
}
我承认这是一个有点疑难杂症的 - 这不是<一个HREF =https://groups.google.com/d/topic/dotliquid/6XDW17YpqLI/discussion>第一这个问题已经提出时间。我还没有拿出一个满意的解决方案,但是任何建议,欢迎。
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.
这篇关于DotLiquid /液体进入词典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!