【问题标题】:Handling FreeMaker template with Ktor Kotlin使用 Ktor Kotlin 处理 FreeMaker 模板
【发布时间】:2018-03-09 08:57:31
【问题描述】:

我对@9​​87654325@(和Java)以及KtorFreeMaker 非常陌生,试图制作一个结合所有这些的应用程序,但看起来我做错了与@ 相关的事情987654328@ 模板操作。

我的应用结构是:

template.ftl:

<#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:

<#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:

打包博客

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:

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) } }
}

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

【问题讨论】:

    标签: kotlin freemarker ktor


    【解决方案1】:

    我得到了答案here

    Template Loading 需要安装为:

    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()方法加载了:

    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 在哪里:

    <!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>
    

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

    【讨论】:

      猜你喜欢
      • 2020-12-27
      • 2018-04-22
      • 2017-08-31
      • 2019-08-13
      • 2021-02-19
      • 2022-07-06
      • 2021-02-16
      • 1970-01-01
      • 2021-07-02
      相关资源
      最近更新 更多