I have a sample code in c#
using System;
using System.Collections.Generic;
using Fluid;
public class StrObj
{
public string Str {get;set;}
}
public class TestObj
{
public List<StrObj> StrObjects {get;set;}
}
public static class Program
{
public static void Main()
{
var templateText = "{% for item in StrObjects %} String: {{ item.StrObj }} {% endfor %}";
var testObj = new TestObj();
testObj.StrObjects = new List<StrObj>();
testObj.StrObjects.Add(new StrObj { Str = "test1" });
testObj.StrObjects.Add(new StrObj { Str = "test2" });
testObj.StrObjects.Add(new StrObj { Str = "test3" });
testObj.StrObjects.Add(new StrObj { Str = "test4" });
var parser = new FluidParser();
if (parser.TryParse(templateText, out IFluidTemplate template, out string error))
{
var ctx = new Fluid.TemplateContext(testObj);
var html = template.Render(ctx);
Console.WriteLine(html);
}
else
{
Console.WriteLine($"Error in html template parser! {error}");
}
}
}
This code should return something like this
String: test1 String: test2 String: test3 String: test4
however, it returns
String: String: String: String:
it writes 4 times “String:” that means for loop in the template works, but why I can’t see the values?
I add this sample to dotnetfiddle, too.
https://dotnetfiddle.net/wIq9mS
Thanks in advance!
New contributor
Neanbey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.