template

Simple templating engine for Kit

Files

FileDescription
kit.tomlPackage manifest with metadata and dependencies
src/template.kitMustache-style templating with loops and conditionals
tests/template-test.kitTests for variables, loops, conditionals, layouts
examples/basic.kitVariables, conditionals, loops, HTML templates
LICENSEMIT license file

Dependencies

No Kit package dependencies.

Installation

kit add gitlab.com/kit-lang/packages/kit-template.git

Usage

import Kit.Template

License

MIT License - see LICENSE for details.

Exported Functions & Types

TemplateError

Template error type with specific variants for different failure modes.

Variants

TemplateParseError {message}
TemplateRenderError {message}

ContextValue

Value types for template context variables.

Variants

StringVal {String}
BoolVal {Bool}
ListVal {List, ContextValue}
ObjectVal {List}
NullVal

empty-context

Creates an empty template context.

List (String, ContextValue)

empty-partials

Creates an empty partials collection.

List (String, String)

with-partial

Adds a partial template to the partials collection.

List (String, String) -> String -> String -> List (String, String)

empty-content-blocks

Creates an empty content blocks collection.

List (String, String)

with-content-block

Adds a content block to the collection.

List (String, String) -> String -> String -> List (String, String)

with-string

Adds a string value to the template context.

List (String, ContextValue) -> String -> String -> List (String, ContextValue)

with-bool

Adds a boolean value to the template context.

List (String, ContextValue) -> String -> Bool -> List (String, ContextValue)

with-list

Adds a list of strings to the template context.

List (String, ContextValue) -> String -> List String -> List (String, ContextValue)

with-object

Adds a nested object to the template context. The object is a list of (key, value) pairs where value is a string.

List (String, ContextValue) -> String -> List (String, String) -> List (String, ContextValue)

with-objects

Adds a list of objects to the template context. Each object is a list of (key, value) pairs where value is a string.

List (String, ContextValue) -> String -> List (List (String, String)) -> List (String, ContextValue)

render

Renders a template string with the given context.

String -> List (String, ContextValue) -> Result String TemplateError

render-with-partials

Renders a template with partials support. partials: A list of (name, template) pairs created with empty-partials/with-partial.

String -> List (String, ContextValue) -> List (String, String) -> Result String TemplateError

render-with-layout

Renders a page template within a layout template.

This implements two-phase rendering: 1. First, the page template is rendered, collecting any {{#content-for name}}...{{/content-for}} blocks 2. Then, the layout template is rendered with the page content available as {{{content}}} and collected blocks available via {{yield name}} or {{#yield name}}default{{/yield}}

Example page template: {{#content-for scripts}} <script src="/js/page.js"></script> {{/content-for}} <h1>Page Content</h1>

Example layout template: <html> <head>{{yield styles}}</head> <body> {{{content}}} {{#yield scripts}}<script src="/js/default.js"></script>{{/yield}} </body> </html>

String -> String -> List (String, ContextValue) -> Result String TemplateError

render-with-layout-and-partials

Renders a page template within a layout template, with partials support.

String -> String -> List (String, ContextValue) -> List (String, String) -> Result String TemplateError

render-with-strings

Renders a template with a simple string map (list of key-value pairs).

String -> List (String, String) -> Result String TemplateError

context-from-strings

Creates a context from a list of string pairs.

List (String, String) -> List (String, ContextValue)