title: Handling FreeMaker template with Ktor Kotlin
body: <p>I'm very new to <code>Kotlin</code> (and Java), as well as <code>Ktor</code> and <code>FreeMaker</code>, trying to make an app combining all of them, but looks I'm doing something wrong related to the <code>FreeMaker</code> templates manipulating.</p>
  
  <p>My app structure is:</p>
  
  <p><a href="https://i.stack.imgur.com/qIFhN.png" rel="noreferrer"><img src="https://i.stack.imgur.com/qIFhN.png" alt="enter image description here"></a></p>
  
  <p><code>template.ftl</code>:</p>
  
  <pre><code>&lt;#macro mainLayout title="Welcome"&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
  
      &lt;title&gt;${title} | Kweet&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  HI
  &lt;/body&gt;
  &lt;/html&gt;
  &lt;/#macro&gt;
  </code></pre>
  
  <p><code>index.ftl</code>:</p>
  
  <pre><code>&lt;#import "template.ftl" as layout /&gt;
  imported title: ${title}
  &lt;@layout.mainLayout title="Welcome"&gt;
  &lt;div class="posts"&gt;
      &lt;h3 class="content-subhead"&gt;Top 10&lt;/h3&gt;
  &lt;/div&gt;
  &lt;/@layout.mainLayout&gt;
  </code></pre>
  
  <p><code>BlogApp.kt</code>:</p>
  
  <p>package blog</p>
  
  <pre><code>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&lt;String&gt;) {
      embeddedServer(Netty, 8080, watchPaths = listOf("BlogAppKt"), module = Application::module).start()
  }
  </code></pre>
  
  <p><code>build.gradle</code>:</p>
  
  <pre><code>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) } }
  }
  </code></pre>
  
  <p>I got the below output upon starting the server:</p>
  
  <p><a href="https://i.stack.imgur.com/1F8kv.png" rel="noreferrer"><img src="https://i.stack.imgur.com/1F8kv.png" alt="enter image description here"></a></p>