ripper234
ripper234

Reputation: 230206

Are Java constants availble for usage from within Play! templates?

I have a User entity:

@Entity
public class User extends PortalModel {

    ...

    public enum Role {
        User,
        Admin,
    }

    @Required
    public Role role;

    public boolean isAdmin() {
        return role == Role.Admin;
    }
}

And I tried to use this template:

#{if user && user.role == User.Role.Admin}
    <li>
        <a href="/admin">Admin</a>
    </li>
#{/if}

This failed, and I suspect the reason is that you can only access the model objects themselves from template, but not other runtime classes, including even enum constants. I moved to defining and using the isAdmin() method:

#{if user && user.isAdmin()}

And this worked. I think I'm right, but would just like a confirmation - is it true that the only java objects you can access from the template are the model classes themselves, that are passed along in render() or put in renderArgs ?

Upvotes: 2

Views: 260

Answers (1)

millimoose
millimoose

Reputation: 39980

The template engine docs for Play! say the expression syntax is Groovy, so this should be possible. Try using the fully qualified name for User.

Upvotes: 3

Related Questions