# odin-mustache Native implementation of {{mustache}} templates in [Odin](https://odin-lang.org). https://github.com/benjamindblock/odin-mustache/assets/1155805/7d794861-e308-4132-aaa0-f800392208a4 All features are implemented, except for the ability to change delimiters. All in tests in the [official mustache spec](https://github.com/mustache/spec) pass successfully (except for the delimiters spec suite). ## Documentation For more information about mustache, see the [mustache project page](https://mustache.github.io) or the mustache [man](https://mustache.github.io/mustache.5.html) [pages](https://mustache.github.io/mustache.1.html). View some [example mustache files](https://github.com/mustache/mustache/tree/master/examples) to get an overview. ## Spec The [mustache-spec](https://github.com/mustache/spec) repo is added as a git submodule for testing purposes. To run tests, ensure that you run `git clone --recursive` and/or `git submodule update` as needed. ## Usage ### CLI Usage ``` Usage: odin-mustache [path to template] [path to JSON] [OPTIONAL - layout] Examples: $ odin-mustache template.txt data.json ``` ### Odin Usage #### 1. `render(template: string, data: any, partials: any, allocator := context.allocator)` Renders a template, provided as a `string`. `data` and `partials` should be *either* a `map[string]...` or a `struct`. All `map` arguments passed **must** be keyed with `string` type data. When parsing a Mustache template, the text inside a tag (eg. `name` inside `{{name}}`) will be parsed as a `string`. #### 2. `render_from_filename(filename: string, data: any, partials: any, allocator := context.allocator)` Renders a template stored in a text file using `data` and `partials` provided. #### 3. `render_with_json(template: string, json_filename: string, allocator := context.allocator)` Renders a template `string` 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. #### Example ```odin input := "Hello, {{name}}!" data: map[string]string = { "name" = "St. Charles", } output, err := render(input, data, context.temp_allocator) // => "Hello, St. Charles!" ``` ## Escaping `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 like `5 > 2` are converted to `5 > 2`. To use raw characters, use three curly brackets `{{{var}}}`. ## Layouts `odin-mustache` supports rendering templates with layouts. A layout is a regular template with a special function. It accepts **a `{{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 (``, `