I am trying to use the example from http://dotliquidmarkup.org/try-online. I have the following code using the NuGet package DotLiquid
namespace TestDotLiquidLoop {
public class User : Drop
{
public string Name { get; set; }
public List<Task> Tasks { get; set; }
}
public class Task
{
public string Name { get; set; }
}
class Program
{
static void Main(string[] args)
{
//Console.WriteLine("Hello World!");
//const string template1 = "{{ user.name | upcase }} has to do:{% for item in user.tasks -%}{ { item.name } }{% endfor -%}";
const string template = "<p>{{ user.name | upcase }} has to do:</p>" +
"< ul >{% for item in user.tasks -%}" +
"< li >{ { item.name } }</ li >" +
"{% endfor -%}" +
"</ ul > ";
Template preparedTemplate = Template.Parse(template);
var user = new User
{
Name = "Tim Jones",
Tasks = new List<Task>
{
new Task { Name = "Documentation" },
new Task { Name = "Code comments" }
}
};
Console.WriteLine(preparedTemplate.Render(DotLiquid.Hash.FromAnonymousObject(new { tempuser = user })));
}
}
}
I am not getting the output correctly. My output is like this.I know its something silly but can anyone help me out with this ?
<p> has to do:</p>< ul ></ ul >
You probably missed new { tempuser = user }
that you renamed anonymous object property to tempuser
at some point. This new { user }
is equivalent to this new { user = user }
. To reference tempuser
inside template you need to just change name.
string template = "<p>{{ tempuser.name | upcase }} has to do:</p>" +
"< ul >{% for item in tempuser.tasks -%}" +
"< li >{{ item.name }}</ li >" +
"{% endfor -%}" +
"</ ul > ";
Template preparedTemplate = Template.Parse(template);
var user = new User
{
Name = "Tim Jones",
Tasks = new List<Task>
{
new Task { Name = "Documentation" },
new Task { Name = "Code comments" }
}
};
Template.RegisterSafeType(typeof(Task), new []{"Name"});
Console.WriteLine(preparedTemplate.Render(DotLiquid.Hash.FromAnonymousObject(new { tempuser = user })));