2018-10-22

(sph lang template)

s-expression template processor.

part of sph-lib

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

variable: sph-lang-template-description
procedure: template-bindings-proc data ->
procedure: template-compose env source ... ->
environment template-source ... -> procedure:template-proc
evaluated templates from source are passed as content to the templates before them like function composition
procedure: template-datum->template-proc a env ->
any:scheme-datum environment -> procedure:template-proc
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
procedure: template-fold fold-proc bindings env source custom-values ... ->
procedure:{procedure any -> any} list environment template-source any -> any
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))
procedure: template-get env a ->
environment procedure/string/port/any -> procedure