2013-04-28 43 views
1

如何在不使用外部模板文件的情況下從Play 2渲染內聯HTML?使用Scala從Play Framework渲染內聯HTML?

def checkStatus = Action { 
    val status = ... 
    if (status.ok) { 
    Ok("Status OK") 
    } else { 
    // Oops, this renders the literal text instead of the HTML I wanted: 

    Ok("Bad status, check out <a href='http://help.us.com/'>our help page</a>") 
    } 
} 

回答

4

Ok("Hello World!")設置Content-Typetext/plain除非明確規定:

Ok("Bad status, check out <a href='http://help.us.com/'>our help page</a>").as(HTML) 

Docs

2

當你渲染視圖,播放識別它的類型(至少對HTML,XML,以及txt),但是當你想返回普通的String時,你需要指示它是哪種類型(否則假設爲text/plain

根據Manipaliting response doc您需要的類型與返回:

BadRequest("Bad status, check out <a href='http://help.us.com/'>our help page</a>").as("text/html")