The solution to this is not using "something like" reflections but to actually use reflections. Any library or framework for this will be most likely based on reflections anyway.
The objects class provides all methods you need to get declared or both declared and inherited fields. The field class contains all information about the actual field, like its name, type or value.
Example
Some class to display on the page:
public class Item {
public String foo = "bar";
public int x = 23;
}
Template:
@(item: Item)
<ul>
@for(field <- item.getClass().getDeclaredFields()) {
<li>@field.getName() = @field.get(item)</li>
}
</ul>
Output:
<ul>
<li>foo = bar</li>
<li>x = 23</li>
</ul>
Keep in mind that the template might not be the right place to do this, especially if you maybe want to filter the fields or something. You should move the code to the controller, or even better to the model. Since you know the object is an Item
, you could create a getFields()
method in Item
where you implement more complex logic and return a list of fields or field names.
The choice of semantic html tags really depends on the content of your fields. Generally speaking, if you only print the field name, a list of fields sounds reasonable. If you print names and values, you could use a definition list dl
or maybe a table
.