DotLiquid 模板引擎示例输出问题

DotLiquid Template Engine Example Output Problem

我正在尝试使用 http://dotliquidmarkup.org/try-online 中的示例。 我有以下代码使用 NuGet 包 DotLiquid

命名空间 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 })));

    }
}

}

我没有得到正确的输出。我的输出就像 this.I 知道它有些愚蠢但是有人可以帮我解决这个问题吗?

<p> has to do:</p>< ul ></ ul >

您可能错过了 new { tempuser = user },您在某个时候将匿名对象 属性 重命名为 tempuser。此 new { user } 等同于此 new { user = user }。要在模板中引用 tempuser,您只需更改名称即可。

        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 })));