首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Ktor Kotlin处理FreeMaker模板

用Ktor Kotlin处理FreeMaker模板
EN

Stack Overflow用户
提问于 2017-09-27 18:49:24
回答 1查看 4.1K关注 0票数 3

我对Kotlin (和Java)以及KtorFreeMaker都很陌生,试图将它们结合在一起制作一个应用程序,但看起来我在做一些与FreeMaker模板操作有关的错误。

我的应用程序结构是:

template.ftl

代码语言:javascript
复制
<#macro mainLayout title="Welcome">
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <title>${title} | Kweet</title>
</head>
<body>
HI
</body>
</html>
</#macro>

index.ftl

代码语言:javascript
复制
<#import "template.ftl" as layout />
imported title: ${title}
<@layout.mainLayout title="Welcome">
<div class="posts">
    <h3 class="content-subhead">Top 10</h3>
</div>
</@layout.mainLayout>

BlogApp.kt

包博客

代码语言:javascript
复制
import kotlinx.html.*
import org.jetbrains.ktor.freemarker.*
import org.jetbrains.ktor.host.*   // for embededServer
import org.jetbrains.ktor.netty.*  // for Netty
import org.jetbrains.ktor.application.*
import org.jetbrains.ktor.features.*
import org.jetbrains.ktor.html.*
import org.jetbrains.ktor.routing.*
import org.jetbrains.ktor.response.*

fun Application.module() {   
    install(DefaultHeaders)
    install(CallLogging)
    install(Routing) {
        get("/") {
           val model = mapOf("id" to 1, "title" to "Hello, World!")
           call.respond(FreeMarkerContent("index.ftl", model, "e"))
        }
    }
}

fun main(args: Array<String>) {
    embeddedServer(Netty, 8080, watchPaths = listOf("BlogAppKt"), module = Application::module).start()
}

build.gradle

代码语言:javascript
复制
group 'Example'

version 'alpha'

buildscript {
    ext.kotlin_version  = '1.1.4-3'
    ext.ktor_version    = '0.4.0'

    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

apply plugin: 'java'
apply plugin: 'kotlin'


sourceCompatibility = 1.8

repositories {
    jcenter()
    mavenCentral()
    maven { url  "http://dl.bintray.com/kotlin/ktor" }
    maven { url "https://dl.bintray.com/kotlin/kotlinx" }
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
    compile "org.jetbrains.ktor:ktor-core:$ktor_version"
    compile "org.jetbrains.ktor:ktor-netty:$ktor_version"
    compile "org.jetbrains.ktor:ktor-html-builder:$ktor_version"

    compile "org.jetbrains.ktor:ktor-freemarker:$ktor_version"

    compile "org.apache.commons:commons-email:1.4"
    compile "org.slf4j:slf4j-simple:1.7.25"
    compile "ch.qos.logback:logback-classic:1.2.1"
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
kotlin {
    experimental {
        coroutines "enable"
    }
}


jar {
    baseName 'abc'
    manifest {
        attributes 'Main-Class': 'blog.BlogAppKt'
    }

    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}

在启动服务器时,我得到了以下输出:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-29 20:36:16

我得到了答案,这里

需要将模板加载安装为:

代码语言:javascript
复制
import org.jetbrains.ktor.freemarker.*
import freemarker.cache.*; // template loaders live in this package

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

然后,可以使用resources/templates方法加载保存在call.respond()中的模板:

代码语言:javascript
复制
val user = mapOf("title" to "Welcome guy", "name" to "user name", "email" to "user@example.com")
call.respond(FreeMarkerContent("index.ftl", user, "e"))

其中index.ftl是:

代码语言:javascript
复制
<!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>

我发现也是一个很好的学习FTL模板的创业公司。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46455185

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档