# Template engine?

**URL:** <https://discuss.kotlinlang.org/t/template-engine/866>\
**Category:** Uncategorized\
**Created:** [December 14, 2012, 11:50pm UTC](https://discuss.kotlinlang.org/t/template-engine/866 "2012-12-14T23:50:36Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![steamstreet](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/steamstreet/32/1529_2.png) [@steamstreet](https://discuss.kotlinlang.org/u/steamstreet)\
**Post date:** [December 14, 2012, 11:50pm UTC](https://discuss.kotlinlang.org/t/template-engine/866/1 "2012-12-14T23:50:36Z")

</div>

Is there any current project working on a Kotlin template engine? Something along the lines of Scala Templates that are used in Play?

---

<div class="post-metadata">

**Author:** ![Flow](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@Flow](https://discuss.kotlinlang.org/u/Flow)\
**Post date:** [December 15, 2012, 6:21pm UTC](https://discuss.kotlinlang.org/t/template-engine/866/2 "2012-12-15T18:21:37Z")

</div>

Sounds fun, but I've to read up on those Scala templates first.

What is your plan? 🙂

---

<div class="post-metadata">

**Author:** ![Flow](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@Flow](https://discuss.kotlinlang.org/u/Flow)\
**Post date:** [December 16, 2012, 1:06pm UTC](https://discuss.kotlinlang.org/t/template-engine/866/3 "2012-12-16T13:06:29Z")

</div>

Rather ambitious project I'd say: [https://github.com/playframework/Play20/wiki/ScalaTemplates](https://github.com/playframework/Play20/wiki/ScalaTemplates)

---

<div class="post-metadata">

**Author:** ![ajselvig](https://avatars.discourse-cdn.com/v4/letter/a/67e7ee/32.png) [@ajselvig](https://discuss.kotlinlang.org/u/ajselvig)\
**Post date:** [December 16, 2012, 10:56pm UTC](https://discuss.kotlinlang.org/t/template-engine/866/4 "2012-12-16T22:56:26Z")

</div>

I've been working on a Kotlin web framework that does html and css templates using Kotlin's [builder-style syntax](http://confluence.jetbrains.net/display/Kotlin/Type-safe+Groovy-style+builders). Not really the same as Scala's templates, but I think they're more powerful since they leverage the language features to make very concise markup. I should have something out on github shortly if you're interested.

---

<div class="post-metadata">

**Author:** ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)\
**Post date:** [December 17, 2012, 9:45am UTC](https://discuss.kotlinlang.org/t/template-engine/866/5 "2012-12-17T09:45:05Z")

</div>

Check out [http://kool.io](http://kool.io)

---

<div class="post-metadata">

**Author:** ![steamstreet](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/steamstreet/32/1529_2.png) [@steamstreet](https://discuss.kotlinlang.org/u/steamstreet)\
**Post date:** [December 17, 2012, 11:26pm UTC](https://discuss.kotlinlang.org/t/template-engine/866/6 "2012-12-17T23:26:03Z")

</div>

I'd looked at that, but would prefer a more traditional template solution.

It seems like Kotlin’s built in template support isn’t that far off. It would need to add some control structures, but that’s about it. It can already do variable substitution.

> `val`` ``s`` ``=`` ``"""`` <html>`` <body>``  $if (x == 0) {``   <h1>X is Zero!</h1>`
> 
> `  }`  
> `  $for (y in items) {`  
> `   <li>$y.name</li>`
> 
> `  }`  
> ` </body>`  
> ` </html>`  
> `“”"`

This doesn’t seem too far from where we’re at with the template engine. I haven’t looked at the code for the current string template support. What kind of effort would this sort of thing require, and is it desirable?

---

<div class="post-metadata">

**Author:** ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)\
**Post date:** [December 18, 2012, 9:19am UTC](https://discuss.kotlinlang.org/t/template-engine/866/7 "2012-12-18T09:19:29Z")

</div>

There are control structures available now in string templates:

``

```kotlin
println(“”"
  Foo: ${if (bar) “bar” else “bar1”}
  Bar
“”")
```

  
To be convenient, loops need some library support:

``

```kotlin
“”“sdfsdf ${buildString { for (i in 1…10) it.append(i)} } sdfsadfg”“”
```

  
Where buildString() is defined as follows:

``

```kotlin
fun buildString(body: (StringBuilder) -> ()): String {
  val sb = StringBuilder()
  body(sb)
  return sb.toString()
}
```

---

<div class="post-metadata">

**Author:** ![steamstreet](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/steamstreet/32/1529_2.png) [@steamstreet](https://discuss.kotlinlang.org/u/steamstreet)\
**Post date:** [December 18, 2012, 7:03pm UTC](https://discuss.kotlinlang.org/t/template-engine/866/8 "2012-12-18T19:03:27Z")

</div>

Wow, that's great. I didn't see that documented anywhere. We're much further along than I thought! :)

I do think it’s important to have a native Kotlin template engine (type and null safe, etc.), so I’ll be watching as things evolve, contributing if possible. Can’t wait to see how this evolves.

---

<div class="post-metadata">

**Author:** ![steamstreet](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/steamstreet/32/1529_2.png) [@steamstreet](https://discuss.kotlinlang.org/u/steamstreet)\
**Post date:** [April 5, 2013, 2:49pm UTC](https://discuss.kotlinlang.org/t/template-engine/866/9 "2013-04-05T14:49:52Z")

</div>

I took a slightly different approach, creating some standard iterators:

> fun For(iterator: Iterator\<Any?\>, out: (v:Any?)-\>String): String {&nbsp;&nbsp;val sb = StringBuilder()&nbsp;&nbsp;iterator.forEach { &nbsp;&nbsp;sb.append(out(it))&nbsp;&nbsp;}&nbsp;&nbsp;return sb.toString()}

Allowing you to do something like:

> return """ &nbsp;&nbsp;\<html\> &nbsp;&nbsp;\<head\> &nbsp;&nbsp;\</head\> &nbsp;&nbsp;\<body\> &nbsp;&nbsp;\<ul\> &nbsp;&nbsp;${For(1..10) {"\<li\>$it\</li\>"}} &nbsp;&nbsp;\</ul\>\</body\> &nbsp;&nbsp;\</html\>&nbsp;&nbsp;"""

I also made a version of While.

This is all good, but Kotlin does not, as of yet, support language injection, which would make this all the better. There was an existing issue related to this:

[http://youtrack.jetbrains.com/issue/KT-2428](http://youtrack.jetbrains.com/issue/KT-2428)

Once that’s implemented, my Kotlin web development dreams would be realized.

---

<div class="post-metadata">

**Author:** ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)\
**Post date:** [April 5, 2013, 3:41pm UTC](https://discuss.kotlinlang.org/t/template-engine/866/10 "2013-04-05T15:41:31Z")

</div>

For HTML, check out [Kara](http://karaframework.com/docs/views.html).

---

<div class="post-metadata">

**Author:** ![steamstreet](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/steamstreet/32/1529_2.png) [@steamstreet](https://discuss.kotlinlang.org/u/steamstreet)\
**Post date:** [April 5, 2013, 4:33pm UTC](https://discuss.kotlinlang.org/t/template-engine/866/11 "2013-04-05T16:33:06Z")

</div>

Yes, I'd looked at that before. While I can see some use cases, for the work I do, I often get html pages sent to me, and the workflow that works best is just to modify that html.

---

<div class="post-metadata">

**Author:** ![luistrigueiros](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/luistrigueiros/32/2161_2.png) [@luistrigueiros](https://discuss.kotlinlang.org/u/luistrigueiros)\
**Post date:** [October 16, 2016, 4:30pm UTC](https://discuss.kotlinlang.org/t/template-engine/866/12 "2016-10-16T16:30:42Z")

</div>

Are there any generic XML builders in Koltin like the ones that exist in Groovy ?

---

<div class="post-metadata">

**Author:** ![vkopichenko](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/vkopichenko/32/1988_2.png) [@vkopichenko](https://discuss.kotlinlang.org/u/vkopichenko)\
**Post date:** [May 12, 2017, 12:56pm UTC](https://discuss.kotlinlang.org/t/template-engine/866/13 "2017-05-12T12:56:19Z")

</div>

```
fun <T> forEach(iterable: Iterable<T>, out: (v: T) -> String): String {
    val sb = StringBuilder()
    iterable.forEach {
        sb.append(out(it))
    }
    return sb.toString()
}
fun <T> forEachIndexed(iterable: Iterable<T>, out: (i: Int, v: T) -> String): String {
    val sb = StringBuilder()
    iterable.forEachIndexed { i, it ->
        sb.append(out(i + 1, it))
    }
    return sb.toString()
}

```

---

<div class="post-metadata">

**Author:** ![soywiz](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/soywiz/32/9083_2.png) [@soywiz](https://discuss.kotlinlang.org/u/soywiz)\
**Post date:** [May 13, 2017, 12:07pm UTC](https://discuss.kotlinlang.org/t/template-engine/866/14 "2017-05-13T12:07:36Z")

</div>

If you are not searching for a specific syntax. I have a project for templates compatibles with twig/django/liquid syntax: [GitHub - korlibs-archive/korte: Kotlin cORoutines Template Engine for Multiplatform Kotlin](https://github.com/soywiz/korte)

---

<div class="post-metadata">

**Author:** ![vkopichenko](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/vkopichenko/32/1988_2.png) [@vkopichenko](https://discuss.kotlinlang.org/u/vkopichenko)\
**Post date:** [June 14, 2017, 9:15pm UTC](https://discuss.kotlinlang.org/t/template-engine/866/15 "2017-06-14T21:15:07Z")

</div>

The easiest out-of-the-box way of using loops in Kotlin string templates I found so far is to replace loops with equivalent [joinToString](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/join-to-string.html) calls:

```
"<ol>${list.joinToString("") { "<li>$it" }}</ol>"

```

or

```
"""
<ol>${list.indices.joinToString("") {
    """
    <li id="item${it + 1}">${list[it]}"""
}}
</ol>""".trimIndent()

```

Crossposted this here: [How to embed for loops in Kotlin string templates - Stack Overflow](https://stackoverflow.com/questions/44554781/how-to-embed-for-loops-in-kotlin-string-templates)

---

<div class="post-metadata">

**Author:** ![pdvrieze](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pdvrieze/32/1882_2.png) [@pdvrieze](https://discuss.kotlinlang.org/u/pdvrieze)\
**Post date:** [June 17, 2017, 10:54am UTC](https://discuss.kotlinlang.org/t/template-engine/866/16 "2017-06-17T10:54:17Z")

</div>

Look at the other parameters to joinToString, there are prefix and postfix parameters which would fit very well with your usecase.

---

<div class="post-metadata">

**Author:** ![lukasjapan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lukasjapan/32/4945_2.png) [@lukasjapan](https://discuss.kotlinlang.org/u/lukasjapan)\
**Post date:** [March 24, 2018, 9:01am UTC](https://discuss.kotlinlang.org/t/template-engine/866/17 "2018-03-24T09:01:07Z")

</div>

I have been working on a HTML template engine for Kotlin, it might be worth taking a look.  
The syntax is similar to jade4j or HAML.

> **[GitHub - lukasjapan/koreander: Kotlin HTML Template Engine](https://github.com/lukasjapan/koreander)**
>
> Kotlin HTML Template Engine. Contribute to lukasjapan/koreander development by creating an account on GitHub.
