- Notifications
You must be signed in to change notification settings - Fork6
Logic-less templates for Odin
License
benjamindblock/odin-mustache
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Native implementation of {{mustache}} templates inOdin.
demo.mp4
All features are implemented, except for the ability to change delimiters.
All in tests in theofficial mustache spec pass successfully (except for the delimiters spec suite).
For more information about mustache, see themustache project page or the mustachemanpages.
View someexample mustache files to get an overview.
Themustache-spec repo is added as a git submodule for testing purposes. To run tests, ensure that you rungit clone --recursive and/orgit submodule update as needed.
Usage: odin-mustache [path to template] [path to JSON] [OPTIONAL - layout]Examples: $ odin-mustache template.txt data.jsonRenders a template, provided as astring.data andpartials should beeither amap[string]... or astruct.
Allmap arguments passedmust be keyed withstring type data. When parsing a Mustache template, the text inside a tag (eg.name inside{{name}}) will be parsed as astring.
Renders a template stored in a text file usingdata andpartials provided.
Renders a templatestring using data and partials stored inside a JSON file.odin-mustache will handle loading the JSON into a usable format for Mustache to work with.
NOTE: The JSON file leverages the following top-level keys:
"data": [required]"partials": [optional]4.render_from_filename_with_json(filename: string, json_filename: string, allocator := context.allocator)
Renders a template stored in a text file using data and partials stored inside a JSON file.odin-mustache will handle loading the JSON into a usable format for Mustache to work with.
input :="Hello, {{name}}!"data:map[string]string = {"name" ="St. Charles",}output, err :=render(input, data,context.temp_allocator)// => "Hello, St. Charles!"
odin-mustache follows the official mustache HTML escaping rules. That is, if you enclose a variable with two curly brackets,{{var}}, the contents are HTML-escaped. For instance, strings like5 > 2 are converted to5 > 2. To use raw characters, use three curly brackets{{{var}}}.
odin-mustache supports rendering templates with layouts.
A layout is a regular template with a special function. It acceptsa{{content}} tag. This is where the output of a child template will be inserted. Layouts have access to the same data provided to the regular template.
Layouts can render content in both{{normal}} and{{{literal}}} tags.
This is helpful for rendering scenarios like websites where the same elements (<head>,<footer>, etc.) are shared across all pages, each of which has their own page-specific template.
<html> {{{head}} <body> {{{content}}} </body> {{{footer}}</html>Usage: odin-mustache [path to template] [path to JSON] [OPTIONAL - layout]Examples: $ odin-mustache template.html data.json layout.htmlTo render a layout in Odin, all four of the above render procedures havein_layout andin_layout_file variations. These methods will insert the rendered content of a template within the given layout. This is convenient for rendering HTML views inside a larger layout, amongst other use-cases.
The full list of corresponding methods is:
render_in_layout(template: string, data: any, layout: string, partials: any, allocator := context.allocator)render_in_layout_file(template: string, data: any, layout_filename: string, partials: any, allocator := context.allocator)render_from_filename_in_layout(filename: string, data: any, layout: string, partials: any, allocator := context.allocator)render_from_filename_in_layout_file(filename: string, data: any, layout_filename: string, partials: any, allocator := context.allocator)render_with_json_in_layout(template: string, json_filename: string, layout: string, allocator := context.allocator)render_with_json_in_layout_file(template: string, json_filename: string, layout_filename: string, allocator := context.allocator)render_from_filename_with_json_in_layout(filename: string, json_filename: string, layout: string, allocator := context.allocator)render_from_filename_with_json_in_layout_file(filename: string, json_filename: string, layout_filename: string, allocator := context.allocator)
template :="Hello, {{name}}."data :=map[string]string{"name" ="Kilgarvan"}layout :=`Above >>{{content}}<< Below`output, _ :=render_in_layout(template, data, layout, allocator :=context.temp_allocator)fmt.println(output)// Above >>// Hello, Kilgarvan.// << Below
odin-mustache works in two steps:
- Lexing and parsing
- Rendering with data
If you are rendering the same template multiple times (ex: sending out personalized emails to subscribers), the lexing+parsing step can be performed once to create a compiled template. This compiled template can then be used multiple times with different data.
src :="Hello, {{name}}!"template: Templateoutput:stringdata:map[string]stringpartials:map[string]stringlexer := Lexer{src=src, delim=CORE_DEF}lexer_parse(&lexer)data = {"name" ="St. Charles"}template = Template{lexer=lexer, data=data, partials=partials}output, _ =template_render(&template)// => Hello, St. Charles!data = {"name" ="Edouard"}template = Template{lexer=lexer, data=data, partials=partials}output, _ =template_render(&template)// => Hello, Edouard!
- Validate JSON keys
- Better CLI argument parsing
- Improve error handling and reporting
- Add optional logging for debugging and performance work
- Configurable precision for floating point types
- Add support for changing delimiters
- Special loop conditionals (eg., checking for the first iteration or last iteration)
About
Logic-less templates for Odin
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Contributors4
Uh oh!
There was an error while loading.Please reload this page.