我在用点液体 http://github.com/formosatek/dotliquid/模板引擎并尝试访问模板中的字典值。
我已经将这个下降传递给模板:
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'] }}
你需要设置Template.NamingConvention
在创建任何放置对象之前。出于性能原因,基Drop
构造函数caches https://github.com/formosatek/dotliquid/blob/master/src/DotLiquid/Drop.cs#L68使用当前命名约定的所有公共实例成员。即使您随后更改命名约定,这些缓存的属性也不会重置。
这段代码对我有用:
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"));
}
我承认这有点棘手——这不是first https://groups.google.com/d/topic/dotliquid/6XDW17YpqLI/discussion这个问题被提出来的时候。我还没有提出令人满意的解决方案,但欢迎任何建议。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)