0

我有這個遊戲的模板,dynamicLink.scala.html ...播放框架模板無法逃脫URL

@(urlWithQuotes: Html, id: Html, toClick: Html) 

@uniqueId_With_Quotes() = { 
    Html("\"" + (@id) + "_" + scala.util.Random.nextInt.toString + "\"") 
} 

@defining(uniqueId_With_Quotes()) { uniqueID => 
    <a [email protected] class="dynamicLink" [email protected]> @toClick </a> 

    <script><!--Do stuff with dynamic link using jQuery--></script> 
} 

它產生了一些JavaScript中的特殊環節。我使這個鏈接是這樣的...

@dynamicLink(
    Html("@{routes.Controller.action()}"), 
    Html("MyID"), 
    Html("Click Me") 
) 

當我呈現它,我得到...

<a id= 
Html("\"" + (MyID) + "_" + scala.util.Random.nextInt.toString + "\"") 
class="dynamicLink" [email protected]{routes.Controler.action()}> Click Me </a> 

這不是我想要呈現的內容。我想渲染此...

<a id="MyID_31734697" class="dynamicLink" href="/path/to/controller/action"> Click Me </a> 

如何使此HTML正確轉義?

*採取#2 - 與字符串*

@(urlWithQuotes: String, id: String, toClickOn: String) 

@uniqueId_With_Quotes() = { 
    Html("\"" + (@id) + "_" + scala.util.Random.nextInt.toString + "\"") 
} 

@defining(uniqueId_With_Quotes) { uniqueID => 
    <a [email protected] class="dynamicLink" [email protected]> @toClickOn </a> 
    ... 
} 

與...

@dynamicLink2(
"@{routes.Controller.action()}", 
"MyID", 
"Click Me" 
) 

呈現更換的Html參數...

<a id= 
    Html("\"" + (MyID) + "_" + scala.util.Random.nextInt.toString + "\"") 
class="dynamicLink" [email protected]{routes.Controller.action()}> Click Me </a> 
    <script> 
     ... 
    </script> 

*更改的Html以字符串不起作用*

*請注意, 「@uniqueId_With_Quotes()」 擴展爲 「HTML(」 \ 「」 +(身份識別碼)+ 「_」 + scala.util.Random.nextInt.toString + 「\」「)「。我希望它實際上執行字符串連接。 *

此外,這應該是顯而易見的,但我希望每個鏈接和隨附的jquery都呈現與該鏈接唯一的ID,我不希望控制器不必擔心分配這些獨特的ID的。我這樣做的方法是在每個id上附加一個隨機數(儘管視圖有一個計數可能會更好)。我需要在視圖中具有這種有狀態行爲,因爲我需要「dynamicLink」對控制器完全透明。

回答

0

您是否嘗試過使用變量作爲字符串類型?

@(urlWithQuotes: String, id: String, toClick: String) 
+0

將Html改爲String不起作用。另外,我不知道什麼時候應該使用Html,什麼時候應該使用String(在用戶表單之外,應該總是使用String來保護注入) –

0

我找到解決方案。你必須傳遞一個Call對象。

@dynamicLink(
    ({routes.Controller.action()}), 
    "MyID", 
    "Click Me" 
    ) 

傳遞這些參數到...

@(urlNoQuotes: Call, id: String = "", toClickOn: String = "") 

@uniqueId_With_Quotes() = @{ 
    Html("\"" + (id) + "_" + scala.util.Random.nextInt.toString + "\"") 
} 

@url() = @{ 
    Html("\"" + urlNoQuotes + "\"") 
} 

@defining(url()) { processedURL => 
    @defining(uniqueId_With_Quotes()) { uniqueID => 
    ... 
    } 
} 

^現在,它的工作原理。