0

I'm looking for a C# template engine which supports iterating through data rows in DataTable. Something like,

var template = Template.Parse(@"
    <ul id='products'>
      {{ for dr in ds.tables[0].rows }}
         <li>
           <h2>{{ dr[0] }}</h2>
           Price: {{ dr[1] }}
         </li>
      {{ end }}
    </ul>
");

var result = template.Render(new { Ds = ds});

I tried Scriban, but it throws error

Scriban.Syntax.ScriptRuntimeException: '<input>(3,31) : error : Expecting a list. 
Invalid value `System.Data.DataTableCollection/DataTableCollection` for the target `ds.tables` for the indexer: ds.tables[0]'

Is there a way to make it work in Scriban, or is there any other template engine which supports DataSet

Baga
  • 1,354
  • 13
  • 24
  • Why don't you create a list from the rows and pass that into your template? – hawkstrider Apr 04 '19 at 15:48
  • Thanks @bhmahler! I was having that as a last option. Not sure of the performance, want to see if there is any engine which supports it by default. – Baga Apr 04 '19 at 16:10
  • I don't know of any that have that level of support, but that does not mean one doesn't exist. Personally, I would create an object for your type, then create a list of said object and use that with your template. – hawkstrider Apr 04 '19 at 16:20
  • Thanks @bhmahler! As you suggested, for now I'm converting the datatable to object as in this [answer](https://stackoverflow.com/questions/7794818/how-can-i-convert-a-datatable-into-a-dynamic-object/7794962#7794962) – Baga Apr 08 '19 at 06:29

0 Answers0