Dictionaries and DotLiquid


I have a basic .NET Dictionary (Dictionary). In my ToLiquid method I do indeed serialize/expose the Dictionary object. My question is how can I ITERATE over the keys in a liquid template just as you would do in regular .NET? It seems as though you MUST know the actual key in order to access the value in the liquid template.

I understand that you could access the value in the liquid template like such


However I don't know the actual key, so I would prefer to use the "for" construct in DotLiquid to iterate over the keys in order to get the various values. Since the "for" construct works on collections and Dictionary is a collection I thought this could be done somehow but all permutations that I have tried have failed.

Any help would greatly be appreciated.


If you only need access to the values in the dictionary, you can do this:

[Test] public void TestForWithDictionary() { var dictionary = new Dictionary<string, string> { { "Graham Greene", "English" }, { "F. Scott Fitzgerald", "American" } }; Helper.AssertTemplateResult(" English American ", "{% for item in authors %} {{ item }} {% endfor %}", Hash.FromAnonymousObject(new { authors = dictionary.Values })); }

However, if you really do need to have access to both keys and values inside the for loop, then this is not supported by the current version (1.6.1) of DotLiquid.


Just create a drop for the dictionary object. Then use it to wrap your members that are dictionaries in your drops. IE:

public class MyDictionaryDrop : Drop { private Dictionary<string,string> _myDictionary; public DictionaryDrop<string, string> MyDictionary { get { return new DictionaryDrop<string, string>(_myDictionary); } } } public class DictionaryDrop<TKey,TValue> : Drop ,IEnumerable { private readonly Dictionary<TKey, TValue> _dictionary; public DictionaryDrop(Dictionary<TKey,TValue> dictionary) { _dictionary = dictionary; } public ICollection<TKey> Keys { get { return _dictionary.Keys; } } public ICollection<TValue> Values { get { return _dictionary.Values; } } public TValue this[TKey key] { get { return _dictionary[key]; } } public IEnumerator GetEnumerator() { return _dictionary.GetEnumerator(); } }


  • Node.js weird encoding on response?
  • converting hash to XML using xmlsimple in ruby
  • How to use cookie , JQuery, Javascript?
  • Azure ServiceBus Token Expired
  • HTMLcollection 0 list length, [n] returns undefined, converting to an array returns an empty array
  • Adding millions of nodes to neo4j spatial layer using cypher and apoc
  • Multiple for loop iterators to unpack in Python [duplicate]
  • What does the “?” mean in the following statement
  • How to convert Neo4j Result to GraphJSON
  • Variant from android-autofittextview library : scaling makes strange behaviour
  • Using multiple input pipelines in TensorFlow
  • Crafting a LINQ based solution to determine if a set of predicates are satisfied for a pair of colle
  • how to remove a div with same ids but display='block' and display='none' in JAVa
  • Many to Many in Linq using Dapper
  • in batch how do i use taskkill properly
  • Local Development, Apache vs Developer - file permissions
  • Accessing Rows In A LINQ Result Without A Foreach Loop?
  • Where these are stored?
  • In Java, how can I construct a File from a resource?
  • Trying to string.Join an IList
  • Configure Spring's MappingJacksonHttpMessageConverter
  • abstracting over a collection
  • How can I tell a form not to dispose a particular control when it closes?
  • Primefaces ManyCheckbox inside ui:repeat calls setter method only for last loop
  • Hash Code in SQL Server?
  • msbuild create itemgroup from property group
  • Updating both a ConcurrentHashMap and an AtomicInteger safely
  • Can't delete or rename original file after resizing
  • Intel-64 and ia32 atomic operations acquire-release semantics and GCC 5+
  • Read text file that is not in the main package in a runnable jar
  • How to recover from a Spring Social ExpiredAuthorizationException
  • ILMerge & Keep Assembly Name
  • Convert array of 8 bytes to signed long in C++
  • Large data - storage and query
  • WOWZA + RTMP + HTML5 Playback?
  • How to pass list parameters for each object using Spring MVC?
  • retrieve vertices with no linked edge in arangodb
  • Understanding cpu registers
  • Recursive/Hierarchical Query Using Postgres
  • Running Map reduces the dimensions of the matrices