From 2f4d45dce241da77f5109e97ba497f0dd4d5c1ec Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 13 Sep 2017 20:18:43 +0200 Subject: [PATCH] Move template rendering extension method to Templates.kt --- src/main/kotlin/net/pterodactylus/sone/utils/Templates.kt | 9 +++++++++ .../kotlin/net/pterodactylus/sone/web/ajax/GetPostAjaxPage.kt | 5 +---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/net/pterodactylus/sone/utils/Templates.kt b/src/main/kotlin/net/pterodactylus/sone/utils/Templates.kt index 8187138..80920a5 100644 --- a/src/main/kotlin/net/pterodactylus/sone/utils/Templates.kt +++ b/src/main/kotlin/net/pterodactylus/sone/utils/Templates.kt @@ -1,7 +1,16 @@ package net.pterodactylus.sone.utils import net.pterodactylus.util.template.Template +import net.pterodactylus.util.template.TemplateContext import net.pterodactylus.util.template.TemplateParser import java.io.StringReader +import java.io.StringWriter fun String.asTemplate(): Template = StringReader(this).use { TemplateParser.parse(it) } + +fun Template.render(templateContext: TemplateContext) = + StringWriter().use { + it.also { + render(templateContext, it) + } + }.toString() diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetPostAjaxPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetPostAjaxPage.kt index 3c35125..eb8d8a0 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetPostAjaxPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetPostAjaxPage.kt @@ -5,11 +5,10 @@ import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.utils.jsonObject import net.pterodactylus.sone.utils.let import net.pterodactylus.sone.utils.parameters +import net.pterodactylus.sone.utils.render import net.pterodactylus.sone.web.WebInterface import net.pterodactylus.sone.web.page.FreenetRequest import net.pterodactylus.util.template.Template -import net.pterodactylus.util.template.TemplateContext -import java.io.StringWriter /** * This AJAX handler retrieves information and rendered representation of a [Post]. @@ -42,5 +41,3 @@ class GetPostAjaxPage(webInterface: WebInterface, private val postTemplate: Temp }.let { postTemplate.render(it) } } - -private fun Template.render(templateContext: TemplateContext) = StringWriter().use { it.also { render(templateContext, it) } }.toString() -- 2.7.4