All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.javalin.plugin.rendering.template.JavalinMustache.kt Maven / Gradle / Ivy

There is a newer version: 6.2.0
Show newest version
/*
 * Javalin - https://javalin.io
 * Copyright 2017 David Åse
 * Licensed under Apache 2.0: https://github.com/tipsy/javalin/blob/master/LICENSE
 */

package io.javalin.plugin.rendering.template

import com.github.mustachejava.DefaultMustacheFactory
import com.github.mustachejava.MustacheFactory
import io.javalin.core.util.OptionalDependency
import io.javalin.core.util.Util
import io.javalin.http.Context
import io.javalin.plugin.rendering.FileRenderer
import java.io.StringWriter

object JavalinMustache : FileRenderer {

    private var mustacheFactory: MustacheFactory? = null
    private val defaultMustacheFactory: MustacheFactory by lazy { DefaultMustacheFactory("./") }

    @JvmStatic
    fun configure(staticMustacheFactory: MustacheFactory) {
        mustacheFactory = staticMustacheFactory
    }

    override fun render(filePath: String, model: Map, ctx: Context): String {
        Util.ensureDependencyPresent(OptionalDependency.MUSTACHE)
        val stringWriter = StringWriter()
        (mustacheFactory ?: defaultMustacheFactory).compile(filePath).execute(stringWriter, model).close()
        return stringWriter.toString()
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy