Pebble extensions
Helpers for building custom Pebble extensions.
Register them through InvirtPebbleConfig.extensions.
pebbleFunction
Builds a PebbleFunction from a Kotlin lambda. The lambda receives a PebbleFunctionExecutionContext
with the call arguments, current template, evaluation context and line number.
- Example
- Declaration
val currentUserFn = pebbleFunction("currentUser") {
request.principal as? User
}
val greetFn = pebbleFunction("greet", "name") {
"Hello, ${args["name"]}"
}
fun pebbleFunction(
name: String,
vararg argumentNames: String,
block: PebbleFunctionExecutionContext.() -> Any?
): PebbleFunction
class PebbleFunctionExecutionContext(
val args: Map<String, Any>,
val template: PebbleTemplate,
val context: EvaluationContext,
val lineNumber: Int
)
pebbleFunctions
Bundles one or more PebbleFunction instances into an Extension that can be passed to
InvirtPebbleConfig.
- Example
- Declaration
Invirt.configure(
pebble = InvirtPebbleConfig(
extensions = listOf(
pebbleFunctions(currentUserFn, greetFn)
)
)
)
fun pebbleFunctions(vararg functions: PebbleFunction): Extension
EvaluationContext.request
Inside a pebbleFunction block, context.request returns the http4k Request for the current render.
A pre-built requestFunction and errorsFunction use this internally to expose request() and
errors() in macros.