2013-12-03 44 views
1

我想使用遊戲模板來生成編程語言的源代碼。玩斯卡拉自定義模板格式

我想按照Play's documentation爲模板引擎添加對自定義格式的支持。但我不明白:

1)在哪裏指定一個新的文件擴展名? templatesTypes += ("code" -> "some.lang.LangFormat") in build.sbt?

2)如何停止逃跑HTML字符,添加空白行

有沒有人得到了與遊戲的自定義模板格式的播放體驗? 如果可能,請提供示例鏈接。

回答

3

1)在你的榜樣將「碼」的擴展,因此,例如,對於蟒蛇模板生成器,你會怎麼做:

templateTypes += ("py" -> "some.python.PythonFormat") 

2)轉義HTML字符的東西是some.lang.LangFormat類,必須執行play.api.template.Format類。這有以下兩種方法:

/** 
* A template format defines how to properly integrate content for a type `T` (e.g. to prevent cross-site scripting attacks) 
* @tparam T The underlying type that this format applies to. 
*/ 
trait Format[T <: Appendable[T]] { 
    type Appendable = T 

    /** 
    * Integrate `text` without performing any escaping process. 
    * @param text Text to integrate 
    */ 
    def raw(text: String): T 

    /** 
    * Integrate `text` after escaping special characters. e.g. for HTML, 「<」 becomes 「&amp;lt;」 
    * @param text Text to integrate 
    */ 
    def escape(text: String): T 
} 

所以你可以讓escape委託raw如果你不想做任何轉義。

就控制換行符而言,這是模板本身的一個方面,如果您在模板中放置換行符,它們將顯示在結果中。因此,例如:

@if(foo) { 
    @bar 
} 

將有換行符,而:

@if(foo) {@bar} 

不會。