我想使用Play模板为编程语言生成源代码。
我想根据Play's documentation向模板引擎添加对自定义格式的支持。但我不明白:
1)在哪里指定新的文件扩展名? templatesTypes += ("code" -> "some.lang.LangFormat")
在build.sbt?
2)如何停止播放转义HTML字符和添加空行
有没有人有Play自定义模板格式的经验? 如果可能,请提供示例链接。
答案 0 :(得分:3)
1)示例中的扩展名为" code",例如,对于python模板生成器,您可以这样做:
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 “&lt;”
* @param text Text to integrate
*/
def escape(text: String): T
}
因此,如果您不想进行任何转义,则可以escape
委托给raw
。
就控制换行而言,这是模板本身的一个方面,如果在模板中放置换行符,它们将出现在结果中。例如:
@if(foo) {
@bar
}
会有换行符,而:
@if(foo) {@bar}
赢得&#39;吨