I have a C# Dictionary<string, Dictionary<string, object>>, and I am trying to display the data with nVelocity template and also I am trying to get the keys of the dictionary with an index. I am newbie.
Please help me with how to access keys with an index e.g. I want to access key at 0 index and also how can I display data with iterations and how do we put "break" statements in foreach loops?
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr class="HeaderOne">
<td align="left">Class</td>
#foreach($services in $VolumeSummary)
#foreach($item in $VolumeSummary[$services])
<td class="numeric-th">$item.AverageUnitPriceF</td>
#end
#break
#end
<td class="numeric-th">Average Price</td>
<td class="numeric-th">Total</td>
</tr>
#foreach ($item in $TotalsByServiceAndTermYear)
<tr style="color:#333;font-weight:normal;">
<td>$item.Service</td>
<td class="numeric-td">
$!item.Year
</td>
<td class="numeric-td">
$!item.AverageUnitPriceF
</td>
<td class="numeric-td">
$!item.TotalPriceF
</td>
</tr>
#end
</tbody>
</table>
NVelocity just sits over the .NET objects so you can't get a item by index from a dictionary, but you can use
$dict.get_Item("key")to get an item by key, which is the syntax for indexers as that is the underlying CLR method for the item indexer.As others have mentioned a
Dictionaryhas a non-deterministic sort order, consider using aSortedDictionary.You can break from a
#foreachusing the#breakdirective, like your example. It was implemented in 2011 after we released v1.1.1, so you'll have to build from master or get a master build from our build server, otherwise you can workaround it by using a conditional to hide the contents.