♻️ Turn new elements properties into methods
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / page / TemplateRenderer.kt
1 package net.pterodactylus.sone.web.page
2
3 import net.pterodactylus.util.template.*
4 import java.io.*
5 import javax.inject.*
6
7 class TemplateRenderer @Inject constructor(private val templateContextFactory: TemplateContextFactory) {
8
9         fun render(template: Template, processor: (TemplateContext) -> Unit = {}): String =
10                         templateContextFactory.createTemplateContext().let { templateContext ->
11                                 templateContext.mergeContext(template.initialContext)
12                                 processor(templateContext)
13                                 StringWriter().also { template.render(templateContext, it) }.toString()
14                         }
15
16 }