Handling FreeMaker template with Ktor Kotlin

本小妞迷上赌 提交于 2019-12-06 14:12:01

I got the answer here.

Template Loading is required to be installed as:

import org.jetbrains.ktor.freemarker.*
import freemarker.cache.*; // template loaders live in this package

install(FreeMarker) {
    templateLoader = ClassTemplateLoader(TheApp::class.java.classLoader, "templates")
}

Then the template saved in resources/templates can be loaded using call.respond() method:

val user = mapOf("title" to "Welcome guy", "name" to "user name", "email" to "user@example.com")
call.respond(FreeMarkerContent("index.ftl", user, "e"))

Where index.ftl is:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <title>${title} | Kweet</title>
</head>
<body>

<@greet person="${name}"!/>

Your email address is ${email}

<#include "/copyright_footer.html">
</body>
</html>

<#macro greet person color="black">
  <font size="+2" color="${color}">Hello ${person}!</font>
</#macro>

I found this also to be a good startup for learning FTL template.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!