gpt4 book ai didi

html - Play 字符串的 scala 模板语法

转载 作者:行者123 更新时间:2023-11-28 01:14:05 25 4
gpt4 key购买 nike

我正在尝试使用 scala 参数驱动此 html anchor 标记的 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} 时,它会正确打印出 key 。

最佳答案

Twirl 不支持 else-if .由于这给您带来了问题,您将其包装在一个动态 block @{} 中,我认为您可以使用它(从未尝试过)。然而,这不是通常的做法,最好改用模式匹配。

您的代码可能如下所示:

@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>
}
}
}
}

当假设消息除了 key 之外都是相同的时,它变得更容易,放弃模式匹配和 href 定义(因为它只使用一次):

@defining(p.getKey()) { key =>
<a href="/authentication/@key">Sign in with @key</a>
}

关于html - Play 字符串的 scala 模板语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36463555/

25 4 0
文章推荐: css - IE11 消息栏从底部飞起而不是从顶部缓入
文章推荐: css - 点击输入外,返回false?
文章推荐: javascript - 如何在 android webview 中使用 js 获取特定的
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com