播放字符串的 scala 模板语法
Play scala template syntax for a string
我正在尝试使用 scala 参数驱动这个 html 锚标记的 href 属性,但似乎无法让它工作。
@{
val key = p.getKey()
if(key == "facebook") {
<a href="/authenticate/@(key)">Sign in with facebook</a>
} else if (key == "twitter"){
<a href="/authenticate/{key}">
<span>Sign in with twitter {key} (this works)</span>
</a>
}
}
在这两个示例中,href 属性都没有正确生成,但是当我在 html 属性之外的 span 标记中使用 {key} 时,它会正确打印出密钥。
Twirl 不支持 else-if。由于这给你带来了问题,你将它包装在一个动态块 @{}
中,我认为你可以工作(从未尝试过)。然而,这不是通常的做法,最好改用模式匹配。
您的代码可能如下所示:
@p.getKey() match {
case "facebook" => {
<a href="/authenticate/@{p.getKey()}">Sign in with facebook</a>
}
case "twitter" => {
<a href="/authenticate/@{p.getKey()}">
<span>Sign in with twitter - key @{p.getKey()} </span>
</a>
}
}
现在可行了,但是您还可以使用 defining(而不是 vals)定义可重用范围的值,以减少 p.getKey
和 href 本身的重复:
@defining(p.getKey()) { key =>
@defining(s"/authentication/$key") { href =>
@key match {
case "facebook" => {
<a href="@href">Sign in with facebook</a>
}
case "twitter" => {
<a href="@href"> <span>Sign in with twitter - key @key</span> </a>
}
}
}
}
当假设消息除了密钥之外都是相同的时,它变得更容易,放弃模式匹配和 href 定义(因为它只使用一次):
@defining(p.getKey()) { key =>
<a href="/authentication/@key">Sign in with @key</a>
}
我正在尝试使用 scala 参数驱动这个 html 锚标记的 href 属性,但似乎无法让它工作。
@{
val key = p.getKey()
if(key == "facebook") {
<a href="/authenticate/@(key)">Sign in with facebook</a>
} else if (key == "twitter"){
<a href="/authenticate/{key}">
<span>Sign in with twitter {key} (this works)</span>
</a>
}
}
在这两个示例中,href 属性都没有正确生成,但是当我在 html 属性之外的 span 标记中使用 {key} 时,它会正确打印出密钥。
Twirl 不支持 else-if。由于这给你带来了问题,你将它包装在一个动态块 @{}
中,我认为你可以工作(从未尝试过)。然而,这不是通常的做法,最好改用模式匹配。
您的代码可能如下所示:
@p.getKey() match {
case "facebook" => {
<a href="/authenticate/@{p.getKey()}">Sign in with facebook</a>
}
case "twitter" => {
<a href="/authenticate/@{p.getKey()}">
<span>Sign in with twitter - key @{p.getKey()} </span>
</a>
}
}
现在可行了,但是您还可以使用 defining(而不是 vals)定义可重用范围的值,以减少 p.getKey
和 href 本身的重复:
@defining(p.getKey()) { key =>
@defining(s"/authentication/$key") { href =>
@key match {
case "facebook" => {
<a href="@href">Sign in with facebook</a>
}
case "twitter" => {
<a href="@href"> <span>Sign in with twitter - key @key</span> </a>
}
}
}
}
当假设消息除了密钥之外都是相同的时,它变得更容易,放弃模式匹配和 href 定义(因为它只使用一次):
@defining(p.getKey()) { key =>
<a href="/authentication/@key">Sign in with @key</a>
}