Crates.io | templito |
lib.rs | templito |
version | 0.4.9 |
source | src |
created_at | 2020-09-08 13:31:13.288898 |
updated_at | 2024-11-26 21:04:51.792175 |
description | A template language library that allows multiple parameters to pass between templates |
homepage | |
repository | https://github.com/storyfeet/templito |
max_upload_size | |
id | 286139 |
size | 134,954 |
The Back end Templating language behind the Siter - a static website generator.
Templito templates largely resemble those of handlebars and Go templates, but they have a few standout features.
Template functions can be closures, so they can have data attached to the functions.
Template and Function Managers are separate from the template and can be switched in and out generically.
Basic structure of a template
{{let title = "Page"}}\
<h1>{{first $title "Default Title"}}</h1>
{{if .cat}}\
{{for k v in .cat}}\
<p>{{$k}} = {{$v}}</p>
{{/for}}\
{{/if}}\
Using Blocks
{{@md}}
Markdown
============================
Everthing between an '@<func>' and '\func' tags will run the function on the result of the contents.
for example the because this code is within an '@md' block, the contents will be treated as markdown.
(Assuming the md function is included by the FuncManager)
Ranges like this will create a bullet point list of items:
{{for k v in .items}}
* {{$v}}
{{- /for}}
{{/md}}
In fact any string to string function can be used as an "@block"
But they are more powerful than that.
{{@let lines}}
apple
cat
sandwhich
{{/let}}\
{{for k v in split $lines}}
<p>{{$v}}</p>
{{/for}}
will output:
<p>apple</p>
<p>cat</p>
<p>sandwhich</p>
Depending on the security situation, you can opt in or out to allowing "exec" functions. ("exec" is not included in the defaults)
{{for k v in split (exec "cat" "path/to/file") "\n"}}
<p>{{$k}} = {{$v}}</p>
{{/for}}
"let"
Create variables (Used in rest of docs) {{let x=3;y=9;list=["fish","dog"]}}
"export"
Export variables can be returned to the calling program in a map of exports
"if","elif" and "else"
Control flow, only resolve the contents if the contition is met otherwise look at the elif and elses {{if gt $x 3}}{{$x}}{{elif gt $y 3}}{{$y}}{{else}}Something else{{/if}} //output: 9
"return"
return a value instead of the main string
"for"
Loop over a list or value. This requires the index, and value be named (eg k and v) {{for k v in $list}}list at {{$k}} equals {{$v}};{{/for}}
//output: list at 0 equals fish;list at 1 equals dog;
"switch" and "case"
choose one option based on the value matched by a pattern
{{- switch $list -}}
{{- case [_,]}}List with second element {{$b}}
{{- case {fish:
"as"
Create a single switch case
{{as $list: [_,]}}{{$b}}{{/as}} //output: dog
"define" and "global"
Create functions either as variables to run or globals that are added to the func manager {{define cat age}}I'm a cat aged {{$age}}{/define}} {{run $cat 55}} //output: I'm a cat aged 55
"@let" and "@export"
Create a local or exported variable from the contents of the block; {{- @let a -}} I love {{for k v in $list}}{{if eq $k 0}} and {{/if}}{{$v}}{{/for}} {{- /let -}} {{$a}} //output: I love fish and dog
Keyword "as" now exists which could possibly break some previous functions of the same name.
Can now use {{as val:pattern}}with pattern captures here{{/as}}
Breaking Change : Functions now require a description to make documentation much easier for users of the system.
Funcmanagers now also provide/require a method to provide those descriptions so they can be printed by any software that uses the system.