Making a Template Engine
for Kotlin
M3 Techtalk #87
2018/2/16
M3 Inc., Lukas Prasuhn
Slide 2
Slide 2 text
About Me
- Software Engineer @ M3
- 12 in Japan from Germany
- Github: https://github.com/lukasjapan
- Tech I like
- Raspberry Pi
- Kotlin/Java
- Ruby
- ...
- Creator of “bt-speaker”
- Bluetooth speaker Daemon for Raspberry Pi
Slide 3
Slide 3 text
- Template Engine for Kotlin
- Features
- HAML style
- High Performance
- Type-Safe!!
- Spring Integration
- https://github.com/lukasjapan/koreander
Slide 4
Slide 4 text
HAML Style
- Whitespace ident based syntax
↓
Slide 5
Slide 5 text
High Performance
- Transpile into Kotlin Code
- Compile to JVM Bytecode
- Render by passing the View Model
Template
Kotlin Code
Compiled JVM Code
HTML
View Model
Slide 6
Slide 6 text
Type Safety
Template
Kotlin Code
Compiled JVM Code
HTML
View Model
Defining the ViewModel type
Type checks, etc
Because of Kotlins KType, type
erasure is not an issue!
forced cast
Slide 7
Slide 7 text
Kotlin Script
- Implementation of JSR-223
- (Scripting for the Java Platform)
- Kotlin implementation
- Run arbitrary Kotlin code
- Compiled Scripts
Slide 8
Slide 8 text
Spring Integration
- https://github.com/lukasjapan/koreander-spring/
- https://github.com/lukasjapan/koreander-spring/blob/master/src/main/kotlin/de/cvguy/kotlin/koreander/
spring/example/KoreanderExample.kt
Slide 9
Slide 9 text
Steps for parsing templates
- (Tokenizing)
- Splitting input by white-space, newlines, etc
- Lexing
- Give meaning to the tokens
- Parsing
- Take the tokens and check for correct syntax
- Build the code out of the Tokens
Slide 10
Slide 10 text
Deploying a Java Library
- JFrog
- create maven repository
- apply plugin to gradle
- upload .jar
- possibility to sync with JCenter
- Including library
Slide 11
Slide 11 text
Example
- View Model is forced
- data class recommended