Reputation: 85
I have a model in play framework
public class XYZ extends Model
{
@Id
public int a;
public String field1;
public String field2;
}
In my index.scala.html
I need to generate field1
and field2
dynamically.
I have an object xyz
of XYZ
class.
I need to get the value of xyz.field1
.
I generate the string field1
dynamically in my code using "field".concat("1")
and now I need to convert this string to a field so as to call xyz.field1
.
I am not able to figure out how to do this conversion in my scala.html
file.
Upvotes: 1
Views: 272
Reputation: 29999
You can use reflections to get a field by its name, even in a template.
@classof[XYZ].getField("field" + fieldNum).get(xyz)
If you have only a two fields, a simple if/else would probably a better way to get the fields values. If it's more complex create a method in your model and use some switch
statement or a map, like Mikesname suggested.
Upvotes: 1