sebastienros/fluid

How do you iterate over a list of objects and access the properties of the current object?

Aaron7897 opened this issue · 1 comments

I am trying to figure out how to iterate of a list of objects and then be able to render the value of multiple properties on the current object.

For example if I have a list of objects like below:

  var people = new List<Person>()
  {
      new()
      {
          Name = "Fred",
          Age= 30
      },
      new()
      {
          Name = "Alice",
          Amount = 32
      }
  };

I'd like to be able to iterate over that list and display the properties of each person.

{% for person in people %}
    Name: {{ person.Name }}
    Age: {{ person.Age }}
{% endfor %}

Pass the people value in a context, and allow the Person class.

// do this once and reuse options
var options = new TemplateOptions();
options.MemberAccessStrategy.Register<Person>();

// do this for each template you render
var context = new TemplateContext();
c.SetValue('people', people);