Deadbolt: show parts of template only for current logged user

巧了我就是萌 提交于 2019-12-08 12:56:38

问题


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?


回答1:


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.




回答2:


You will ultimately need to make the same logical checks that you're doing in the above snippet, so you can just create a new "tag" (another view) that takes in the "current" user and does both tests. You'll also probably want to create something similar to use on the controller side, like an extension of the SubjectPresentAction.



来源:https://stackoverflow.com/questions/19978971/deadbolt-show-parts-of-template-only-for-current-logged-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!