Using Mustache Templates

预计阅读时间:1分钟

Ktor通过Moustache功能包括对Moustache模板的支持. 初始化用胡子功能MustacheFactory

    install(Mustache) {
        mustacheFactory = DefaultMustacheFactory("templates")
    }

此MustacheFactory设置Mustache以在"模板"包中相对于当前类路径的类路径上查找模板文件. 基本模板如下所示:

此功能定义在类io.ktor.mustache.Mustache在神器io.ktor:ktor-mustache:$ktor_version .
dependencies { implementation "io.ktor:ktor-mustache:$ktor_version" }
dependencies { implementation("io.ktor:ktor-mustache:$ktor_version") }
<project> ... <dependencies> <dependency> <groupId>io.ktor</groupId> <artifactId>ktor-mustache</artifactId> <version>${ktor.version}</version> <scope>compile</scope> </dependency> </dependencies> </project>
<html>

<h1>Hello </h1>

</html>

使用resources/templates ,可以使用call.respond()方法在应用程序的其他位置访问该模板:

    get("/{...}") {
        val user = User("user name", "user@example.com")
        call.respond(MustacheContent("todos.hbs", mapOf("user" to user)))
    }

by  ICOPY.SITE