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

invirt.pebble.functions.base.kt Maven / Gradle / Ivy

There is a newer version: 0.10.11
Show newest version
package invirt.pebble.functions

import io.pebbletemplates.pebble.extension.Function
import io.pebbletemplates.pebble.template.EvaluationContext
import io.pebbletemplates.pebble.template.PebbleTemplate

abstract class PebbleFunction(val name: String) : Function

abstract class NoArgsPebbleFunction(name: String) : PebbleFunction(name) {
    override fun getArgumentNames() = emptyList()
}

class PebbleFunctionExecutionContext(
    val args: Map,
    val template: PebbleTemplate,
    val context: EvaluationContext,
    val lineNumber: Int
)

fun pebbleFunction(name: String, vararg argumentNames: String, block: PebbleFunctionExecutionContext.() -> Any?): PebbleFunction {
    return object : PebbleFunction(name) {
        override fun getArgumentNames() = argumentNames.toList()

        override fun execute(args: MutableMap, self: PebbleTemplate, context: EvaluationContext, lineNumber: Int): Any? {
            return block(
                PebbleFunctionExecutionContext(
                    args = args,
                    template = self,
                    context = context,
                    lineNumber = lineNumber
                )
            )
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy