死锁:仅显示当前登录用户的模板部分 [英] 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>
}
}
其中userProfile
是当前页面的所有者用户,而userLogged
是实际登录的用户.
where userProfile
is the owner user of the current page, and userLogged
is the actual logged user.
考虑到我将不得不多次进行此检查,Deadbolt或Scala中是否有更好的方法(更清洁)?
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?
推荐答案
按照David的建议,您可以将其包装在自己的标签中.标签只是功能,看起来就像其他视图(实际上,它们是其他视图).
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).
您可以尝试类似的
@(userProfile: User, userLogged: User)(body: => Html)
@subjectPresent() {
@if(userProfile == userLogged){
@body
}
}
并将其保存在名为foo.scala.html
and save this in a file called foo.scala.html
然后可以将其与
@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.
这篇关于死锁:仅显示当前登录用户的模板部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!