2013-12-03 45 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} 

不会。