Skip to content

Templating (Mustache)

Templating is when you return an HTML page with custom data. You can probably tell why that's useful.

Crow supports mustache for templates through its own implementation crow::mustache.

Note

Currently Crow's Mustache implementation is not linked to the Crow application, meaning if an executable has more than one Crow application they'll be sharing any variables or classes relating to template loading and compiling.

Components of mustache

There are 2 components of a mustache template implementation:

  • Page
  • Context

Page

The HTML page (including the mustache tags). It is usually loaded into crow::mustache::template_t. It needs to be placed in the templates directory which should be directly inside the current working directory of the crow executable.

The templates directory is usually called templates, but can be adjusted per Route (via crow::mustache::set_base("new_templates_directory")), per Blueprint, or globally (via crow::mustache::set_global_base("new_templates_directory"")).

For more information on how to formulate a template, see this mustache manual.

Context

A JSON object containing the tags as keys and their values. crow::mustache::context is actually a crow::json::wvalue.

Note

crow::mustache::context can take a C++ lambda as a value. The lambda needs to take a string as an argument and return a string, such as ctx[lmd] = [&](std::string){return "Hello World";};.

Note

The string returned by the lamdba can contain mustache tags, Crow will parse it as any normal template string.

Returning a template

To return a mustache template, you need to load a page using auto page = crow::mustache::load("path/to/template.html");. Or just simply load a string using auto page = crow::mustache::compile("my mustache {{value}}");. Keep in mind that the path is relative to the templates directory.

Note

You can also use auto page = crow::mustache::load_text("path/to/template.html"); if you want to load a template without mustache processing.

Warning

The path to the template is sanitized by default. it should be fine for most circumstances but if you know what you're doing and need the sanitizer off you can use crow::mustache::load_unsafe() instead.


You also need to set up the context by using crow::mustache::context ctx;. Then you need to assign the keys and values, this can be done the same way you assign values to a JSON write value (ctx["key"] = value;).
With your context and page ready, just return page.render(ctx);. This will use the context data to return a filled template.
Alternatively you could just render the page without a context using return page.render();.

Note

page.render(); returns a crow::returnable class in order to set the Content-Type header. to get a simple string, use page.render_string() instead.