2017-09-23

(sph lang template)

s-expression template processor.

part of sph-lib

features

templates can be procedures or read from external files

templates content is interpreted as the content of a quasiquoted list

using unquote, the results of arbitrary scheme expressions can be inserted

configurable, language-specific code unquote execution environments

template variables

template composition using variables or a second "content" parameter

the general result can be anything, for example a string from a sxml

template-fold procedure that allows to specify template sources as ports/strings/procedures and merging or wrapping/nesting depending on structure, (merge-all-on-this-level ...)

or ((wrap-all-on-this-level ...) to-merge ...)

templates are read with the default scheme reader to robustly support all scheme syntax. scheme comment syntax is supported

examples

(template-fold (l (a result) (display a port) result) source bindings env target)

used for example in sph-web-app client.scm

respond-html

respond-html from web-app uses it. the last argument is a template-source argument to template-fold

(respond-html #f "testfile")
(respond-html #f (list "append-this-file" "and-this-file" "and-this-one"))
(respond-html #f (list "append-this-file-with" (list "into-this-file" "insert-this-file" "insert-insert-this-file") "append-this"))
(respond-html #f (list (list (list (quote div) "sxml starts here"))))
(respond-html #f (quote (((div "sxml starts here")))))
(respond-html #f (list (list (lambda (ref content) sxml) "testfile-for-content")))
(respond-html (quote ((myvariable . 2) (othervariable . 3))) (list (lambda (ref content) (ref (quote myvariable)))))

library description

a template engine that interprets expressions as elements of a quasiquoted list.

supports concatenation and composition. source data can be given as files, ports or datums.

creates template procedures.

alternative name: s-template

# data structures

template-procedure :: procedure:{symbol:key [default] -> variable-value} any:content -> any

template-source: element/(element/(element ...) ...)

template-source: single-elements/(concatenated-elements/(composed-elements ...) ...)

element: string:path/procedure:template-procedure/port/any

import name

(sph lang template)

exports

sph-lang-template-description

variable

template-bindings-proc

procedure

signature

data ->

template-compose

procedure

signature

env source ... ->

environment template-source ... -> procedure:template-proc

description

evaluated templates from source are passed as content to the templates before them like function composition

template-datum->template-proc

procedure

signature

a env ->

any:scheme-datum environment -> procedure:template-proc

description

creates a template-procedure from an unevaluated template scheme datum.

additionally to the bindings from the given environment, the bindings "v" and "content" are available in scope.

v can be used as (v (quote variable-name)) to insert template variables. content contains the value

eventually inserted via composition

template-fold

procedure

signature

fold-proc bindings env source custom-values ... ->

procedure:{procedure any -> any} list environment template-source any -> any

description

template-fold processes multiple template sources. it creates the s-expression structure

with variables replaced and passes individual expressions to fold-proc which can then process them further,

for example translate sxml to an xml string.

how sources are combined depends on the nesting depth they are specified with.

first nesting level: (a b c ...)

templates are appended

second nesting level: (a (b c d) ...)

templates are composed/inserted. similar to function application, template b receives the result of template c,

which receives the result of template d. this is appended to a

source elements can be filesystem path strings, template procedures or other scheme datums.

if the datums to insert are lists, then they should be nested in two lists (((content ...))) to avoid the automatic

concatenation or composition.

example call

(template-fold

(l (template-result . custom-values) (pair template-result custom-values))

(list "file.sxml" "appended-file.sxml" (list "layout.sxml" "inserted-into-layout.sxml"))

(alist-q a 1)

(environment (rnrs base))

(list))

template-get

procedure

signature

env a ->

environment procedure/string/port/any -> procedure


tags: programming guile documentation library scheme sph-lib q1 lang-template highlight sph-lang-template