Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nadako/hxmustache
Minimal templating with {{mustaches}} in Haxe http://mustache.github.com/
https://github.com/nadako/hxmustache
haxe mustache template-engine
Last synced: 11 days ago
JSON representation
Minimal templating with {{mustaches}} in Haxe http://mustache.github.com/
- Host: GitHub
- URL: https://github.com/nadako/hxmustache
- Owner: nadako
- License: mit
- Created: 2016-03-31T10:57:58.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2023-04-20T09:54:02.000Z (over 1 year ago)
- Last Synced: 2024-10-25T09:50:00.666Z (about 2 months ago)
- Topics: haxe, mustache, template-engine
- Language: Haxe
- Size: 141 KB
- Stars: 39
- Watchers: 6
- Forks: 5
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![Build Status](https://travis-ci.org/nadako/hxmustache.svg?branch=master)](https://travis-ci.org/nadako/hxmustache)
# Mustache templates for Haxe
This is a Haxe implementation of logic-less [mustache](http://mustache.github.io/) templating.
Originally ported from [mustache.js](https://github.com/janl/mustache.js).
**Status**: works fine, passes tests and should be safe to use. Travis-tested on all Haxe targets (except the new Lua one).
Internal structure and API may change a little, but not much.[Try online!](http://nadako.github.io/hxmustache/)
## Usage
Here's a quick example:
```haxe
class Main {
static function main() {
var template = "Hello {{name}}, how are you?";
var context = {name: "World"};
var output = Mustache.render(template, context);
trace(output); // Hello World, how are you?
}
}
```See [mustache(5)](http://mustache.github.io/mustache.5.html) for the actual template language description, here we'll
only document extensions and API specifics.## API
The main entry point is the `Mustache.render` function and it's defined like this:
```haxe
public static function render(template:String, context:mustache.Context, ?partials:mustache.Partials):String;
```The `template` argument is obviously the template itself. It will be parsed to an AST and cached across calls.
The `context` is the root context for template variables. You can pass any object and its fields will be looked up
using reflection. If field is a function, it will be called without arguments on lookup to return a value to render.
If the object passed is instance of `haxe.ds.StringMap` it contents will be accessed via `get` call rather than reflection.
For an advanced usage, you can manually create `new mustache.Context(yourData)`.The `partials` (optional) is where partial templates are stored (used by `{{>name}}` and `{{String` signature.
## Falsy values
Mustache uses concept of falsy values for e.g. determining if section should be rendered or not.
Here's what hxmustache considers *falsy*:* `null`
* `false`
* `0` and `0.0`
* empty `String`
* empty `Array`These values will make `{{#name}}` sections NOT render, and `{{^name}}` DO render. If used as `{{value}}`s, they won't be rendered in any way.
All other values are considered truthy.
## Functions
If the value of a section is a function, it will be called with 2 arguments:
* part of template inside the section
* rendering function (`String->String`)...and is expected to return rendered string. Example:
```haxe
class Main {
static function main() {
var context = {
"name": "Tater",
"bold": function() { // this function will be called when looking up `bold`
return function(text, render) { // this function will be called for rendering a section
return "" + render(text) + "";
}
}
};
var template = "{{#bold}}Hi {{name}}.{{/bold}}";
var output = Mustache.render(template, context);
trace(output); // Hi Tater.
}
}
```## Additional features
### Template inheritance
hxmustache implements the popular [template inheritance proposal](https://github.com/mustache/spec/pull/75).
Example:```haxe
class Main {
static function main() {
var layout = "
{{$title}}Default title{{/title}}
{{$content}}{{/content}}
";var template = "
{{
Dan
Hello, Dan!
*/
}
}
```## Command-line interface
hxmustache can be used as a command-line tool. Examples:
Output to stdout:
```
haxelib run hxmustache view.json template.mustache
```Output to `output.html`:
```
haxelib run hxmustache view.json template.mustache output.html
```Add partial templates:
```
haxelib run hxmustache view.json template.mustache -p mypartial.mustache -p myotherpartial.mustache
```Partials will be available by the name of the partial template file without directory and extension (e.g. `layout` for `templates/layout.mustache`).