mustache

Mustache template engine for D

Implemented according to mustach(5).

License:
Boost License 1.0.

Authors:
Masahiro Nakagawa

class MustacheException: object.Exception;
Exception for Mustache

struct MustacheEngine(String = string) if (isSomeString!(String));
Core implementation of Mustache

String parameter means a string type to render.

Example:
 alias MustacheEngine!(string) Mustache;

 Mustache mustache;
 auto context = new Mustache.Context;

 context["name"]  = "Chris";
 context["value"] = 10000;
 context["taxed_value"] = 10000 - (10000 * 0.4);
 context.useSection("in_ca");

 write(mustache.render("sample", context));
sample.mustache:
 Hello {{name}}
 You have just won ${{value}}!
 {{#in_ca}}
 Well, ${{taxed_value}}, after taxes.
 {{/in_ca}}

Output:
 Hello Chris
 You have just won 0000!
 Well, 000, after taxes.

enum CacheLevel;
Cache level for compile result

no
No caching

check
Caches compiled result and checks the freshness of template

once
Caches compiled result but not check the freshness of template

struct Option;
Options for rendering

string ext;
template file extenstion

string path;
root path for template file searching

CacheLevel level;
See CacheLevel

Handler handler;
Callback handler for unknown name

class Context;
Mustache context for setting values

Variable:
 //{{name}} to "Chris"
 context["name"] = "Chirs"
Lists section("addSubContext" name is drived from ctemplate's API):
 //{{#repo}}
 //<b>{{name}}</b>
 //{{/repo}}
 //  to
 //<b>resque</b>
 //<b>hub</b>
 //<b>rip</b>
 foreach (name; ["resque", "hub", "rip"]) {
     auto sub = context.addSubContext("repo");
     sub["name"] = name;
 }
Variable section:
 //{{#person?}}Hi {{name}}{{/person?}} to "Hi Jon"
 context["person?"] = ["name" : "Jon"];
Lambdas section:
 //{{#wrapped}}awesome{{/wrapped}} to "<b>awesome</b>"
 context["Wrapped"] = (string str) { return "<b>" ~ str ~ "</b>"; };
Inverted section:
 //{{#repo}}<b>{{name}}</b>{{/repo}}
 //{{^repo}}No repos :({{/repo}}
 //  to
 //No repos :(
 context["foo"] = "bar";  // not set to "repo"

const nothrow String opIndex(in String key);
Gets key's value. This method does not search Section.

Parameters:
String key key string to search

Returns:
a key associated value.

Throws:
a RangeError if key does not exist.

void opIndexAssign(T)(T value, in String key);
Assigns value(automatically convert to String) to key field.

If you try to assign associative array or delegate, This method assigns value as Section.

Parameters:
value some type value to assign
key key string to assign

void useSection(in String key);
Enable key's section.

Parameters:
String key key string to enable

NOTE:
I don't like this method, but D's typing can't well-handle Ruby's typing.

Context addSubContext(in String key, lazy size_t size = 1);
Adds new context to key's section. This method overwrites with list type if you already assigned other type to key's section.

Parameters:
String key key string to add
size_t size reserve size for avoiding reallocation

Returns:
new Context object that added to key section list.

const const(string) ext();
void ext(string ext);
Property for template extenstion

const const(string) path();
void path(string path);
Property for template searche path

const const(CacheLevel) level();
void level(CacheLevel level);
Property for cache level

const const(Handler) handler();
void handler(Handler handler);
Property for callback handler

String render(in string name, in Context context);
Renders name template with context.

This method stores compile result in memory if you set check or once CacheLevel.

Parameters:
string name template name without extenstion
Context context Mustache context for rendering

Returns:
rendered result.

Throws:
object.Exception if String alignment is mismatched from template file.

String renderString(in String src, in Context context);
string version of render.