mstch is a complete implementation of {{mustache}} templates using modern C++
Find a file
2015-04-24 14:47:12 +02:00
include/mstch more documentation 2015-04-24 14:47:12 +02:00
src cmake install 2015-04-24 01:48:42 +02:00
test more documentation 2015-04-24 14:47:12 +02:00
.gitignore moved to lambda tests 2015-04-12 17:12:52 +02:00
.travis.yml use gcc 4.9 2015-04-22 12:35:34 +02:00
CMakeLists.txt reformat 2015-04-23 12:54:08 +02:00
LICENSE Initial commit 2015-04-09 20:14:44 +02:00
README.md more documentation 2015-04-24 14:47:12 +02:00

mstch - {{mustache}} templates in C++11

mstch logo

mstch is a complete implementation of {{mustache}} templates using modern C++.

Build Status

Supported features

mstch supports the complete feature set described in the mustache(5) manpage:

  • JSON-like data structure using Boost.Variant
  • variables, sections, inverted sections
  • partials
  • changing the delimiter
  • C++11 lambdas
  • C++ objects as view models

Basic usage

#include <iostream>
#include <mstch/mstch.hpp>

int main() {
  std::string view{"{{#names}}Hi {{name}}!\n{{/names}}"};
  mstch::map context{
    {"names", mstch::array{
      mstch::map{{"name", std::string{"Chris"}}},
      mstch::map{{"name", std::string{"Mark"}}},
      mstch::map{{"name", std::string{"Scott"}}},
    }}
  };
  
  std::cout << mstch::render(view, context) << std::endl;
  
  return 0;
}

The output of this example will be:

Hi Chris!
Hi Mark!
Hi Scott!

Data structure

The types in the example above, mstch::array and mstch::map are actually aliases for standard types:

using map = std::map<const std::string, node>;
using array = std::vector<node>;

mstch::node is a boost::variant that can hold a std::string, int, bool, lambda expression or a std::shared_ptr to a 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.

Advanced usage

Partials

TODO

Lambdas

TODO

Objects

TODO

Requirements

  • A C++ compiler with decent C++11 support. Currently only tested with GCC 4.9.
  • Boost 1.54+ for Boost.Variant
  • CMake 2.8+ for building

Installing

From the root of the source tree:

 $ mkdir build
 $ cd build
 $ cmake ..
 $ make
 $ make install

Running the unit tests

Unit tests are using the Catch framework, included in the repository. Boost.Program_Options and The Boost Algorithm Library are also required to build them.

 $ mkdir build
 $ cd build
 $ cmake -DWITH_UNIT_TESTS=ON ..
 $ make
 $ make test