【问题标题】:dotLiquid Nested loop not workingdotLiquid 嵌套循环不起作用
【发布时间】:2016-10-13 02:50:44
【问题描述】:

我创建了一个带有两个嵌套 for 循环的 dotLiquid 模板

{% for item in page.Fields %}
        <li>
            {{ item.Name }}:
            <select>
                {% for list in item.ListValues %}
                    <option>{{ list.Text }} </option>
                {% endfor %}
            </select>
        </li>
    {% endfor %}

使用以下类:

public class Page: Drop
{
    public string Name { get; set; }
    public List<Field> Fields { get; set; }
}

public class Field : Drop
{
    public string Name { get; set; }
    public List<ListValue> ListValues { get; set; }
}

public class ListValue : Drop
{
    public string Text { get; set; }
}

然后我创建了一个对象并像这样运行它:

page = MyPage; // This is the created object
Template template = Template.Parse(LiquidTemplate);
string output = template.Render(Hash.FromAnonymousObject(new { page = this.page }));

对象填充为:

new Page
        {
            Name = "My Page",
            Fields = new List<Field>
            {
                new Field
                {
                    Name = "Title",
                    ListValues = new List<ListValue>()
                },
                new Field
                {
                    Name = "Status",
                    ListValues = new List<ListValue>
                    {
                        new ListValue
                        {
                            Text = "Open"
                        }
                    }
                }
            }

        };  

即使对象很好,内部的 for 循环也不会被填充。我看到很多空的内循环&lt;option&gt;标签...

我刚开始使用 dotLiquid,我做错了什么?

【问题讨论】:

  • 试试IList&lt;ListValue&gt; ListValues
  • 试过了,结果一样

标签: c# dotliquid


【解决方案1】:

此解决方案来自 dotliquid 的开发者(我在github project 的问题列表中发布了相同的问题)

dotliquid 默认使用ruby convention

【讨论】:

  • 这是正确的答案。非常感谢!!
猜你喜欢
  • 1970-01-01
  • 2016-04-02
  • 2017-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多