Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dotLiquid Nested loop not working

Tags:

c#

dotliquid

I have created a dotLiquid template with two nested for loop

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

With the following class:

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

Then I created an object and ran it like this:

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

The object is populated as:

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"
                        }
                    }
                }
            }

        };  

The inner for loop is not getting populated, even though the object is just fine. I see a lot of empty inner loop <option> tags...

I have just started with dotLiquid, what am I doing wrong?

like image 257
Samuel Avatar asked Jan 22 '26 17:01

Samuel


1 Answers

This solution is from the developer of the dotliquid (I posted the same question on the issues list of the github project)

dotliquid uses the ruby convention by default.

like image 82
Samuel Avatar answered Jan 24 '26 07:01

Samuel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!