scalasessionplayframeworkplayframework-2.2deadbolt

Deadbolt: show parts of template only for current logged user


In my system every user has an own public profile. I want to show an "Edit" button only on the profile page of the current logged user. Now I'm doing this by using this code

  @subjectPresent() {
    @if(userProfile == userLogged){
      <button>Edit</button>
    }
  }

where userProfile is the owner user of the current page, and userLogged is the actual logged user.

Considering that I will have to do this check a lot of times, is there in Deadbolt or Scala a better (cleaner) way to do it?


Solution

  • As David suggested, you can wrap this up in your own tag. Tags are just functions, and look like other views (in fact, they are other views).

    You can try something like

    @(userProfile: User, userLogged: User)(body: => Html)
    
    @subjectPresent() {
      @if(userProfile == userLogged){
        @body
      }
    }
    

    and save this in a file called foo.scala.html

    You can then use this with

    @foo(userProfile, userLogged) {
      <button>Edit</button>
    }
    

    You'll need to use the correct type declarations or imports where necessary, e.g. User, importing the tag, etc. This depends on the structure of your project.