Building a Template Engine: Mustache Meets Liquid in C

How CAX template engine supports {{variables}}, {% for %}, {% if %}, and nested layouts.

Building a Template Engine: Mustache Meets Liquid in C

Goals

We needed:

  • {{hero.title}} nested lookup
  • {% for post in pagination.items %}
  • {% include header.cax %}
  • --- layout: base.cax --- chaining

Nested Lookup

const char* get_nested_value(ctx, "hero.title"){
  obj = get_pointer(ctx, "hero");
  return get_string(obj, "title");
}

Layout Chaining

cax_template_render_file checks if file starts with ---. If yes, it renders body, then injects as {{content}} into parent layout. Just like Jekyll.

Clean, fast, no regex hell.