|
Electroneum
|
mstch is a complete implementation of {{mustache}} templates using modern C++. It's compliant with specifications v1.1.3, including the lambda module.
mstch supports the complete feature set described in the mustache(5) manpage:
The output of this example will be:
The types in the example above, mstch::array and mstch::map are actually aliases for standard types:
mstch::node is a boost::variant that can hold a std::string, int, double, bool, mstch::lambda or a std::shared_ptr<mstch::object> (see below), also a map or an array recursively. Essentially it works just like a JSON object.
Note that when using a std::string as value you must explicitly specify the type, since a const char* literal like "foobar" would be implicitly converted to bool. Alternatively you can use C++14 string_literals if your compiler supports it.
Partials can be passed in a std::map as the third parameter of the mstch::render function:
Output:
C++11 lambda expressions can be used to add logic to your templates. Like a const char* literal, lambdas can be implicitly converted to bool, so they must be wrapped in a mstch::lambda object when used in a mstch::node. The lambda expression passed to mstch::lambda must itself return a mstch::node. The returned node will be rendered to a string, then it will be parsed as a template.
The lambda expression accepts either no parameters:
Output:
Or it accepts a const std::string& that gets the unrendered literal block:
Output:
Custom objects can also be used as context for rendering templates. The class must inherit from mstch::object, and register it's exported methods with register_methods. Exported methods must have the return type of mstch::node. Objects must be created as a std::shared_ptr.
Output:
By default, mstch uses HTML escaping on the output, as per specification. This is not useful if your output is not HTML, so mstch provides a way to supply your own escape implementation. Just assign any callable object to the static mstch::config::escape, which is an initially empty std::function<std::string(const std::string&)>.
For example you can turn off escaping entirely with a lambda:
If you are using CMake, the easiest way to include mstch in your project is to copy the whole directory to your source tree, and use add_subdirectory in your CMakeLists.txt. This will set a variable named mstch_INCLUDE_DIR that contains its include path, and add a static library target named mstch. For example:
If you prefer to install the library globally, you can simply do the following from the root of the source tree:
The install command may require root privileges. This will also install CMake config files, so you can use use find_package in your CMakeLists.txt:
Unit tests are using the Catch framework and rapidjson to parse the Mustache specifications, all of which are included in the repository as git submodules. Various Boost libraries are also required to build them.
Don't forget to initialize submodules:
To build and run the unit tests:
mstch is licensed under the MIT license.
1.8.14