Back to templates

Template details

hypertextcpp

An HTML templating system for C++ that transpiles .htcpp files into C++ rendering code.

About this template

hypertextcpp is an HTML templating system for C++ applications that transpiles an .htcpp template file into C++ code for rendering HTML.

What is hypertextcpp?

hypertextcpp is a command-line tool that converts .htcpp template files into C++ rendering code. It accepts a template containing HTML markup with embedded C++ expressions, statements, control-flow extensions, sections, and procedures, and produces either a single C++ header, a header-plus-source pair, or a shared-library source file. The tool is developed by kamchatka-volcano and is distributed under the MS-PL license. Generated code targets C++17 and integrates with CMake through provided helper functions.

Key Features

  • C++ expressions — Insert any C++ expression whose result streams to std::ostream directly into the HTML using $(...) syntax, such as $(cfg.name).
  • C++ statements — Embed arbitrary C++ code, like variable declarations or lambdas, within the rendering function using a statement marker.
  • Global statements — Place includes and function definitions outside the rendering function so they appear once in the generated file.
  • Conditional extension — Attach ?(condition) to an opening or closing tag to render that element only when the condition is true.
  • Loop extension — Attach @(auto item : range) or a classic for-loop specification to a tag to repeat the element over a range or loop.
  • Sections — Wrap document fragments in [[ ... ]] with an optional condition to control attribute inclusion or partial rendering.
  • Procedures — Define parameterless functions named with #name() that capture the cfg variable and can be called directly from C++ to render partial content.
  • Multiple generation modes — Generate header-only renderers, header-and-source renderers, or shared-library source files; CMake functions hypertextcpp_GenerateHeader, hypertextcpp_GenerateHeaderAndSource, and hypertextcpp_BuildSharedLibrary are provided.

Who is it for?

C++ developers who want compile-time checked HTML templates without a runtime template engine. It suits teams building web applications in C++ that need fast, type-safe rendering, as well as developers generating static HTML pages from C++ programs. The shared-library mode also fits projects that need to update page templates without recompiling the entire application.

What can you do with it?

  • C++ web developers: render HTML views from C++ data structures with full compile-time type safety, since the template is transpiled to C++ code.
  • Tool builders: create reusable template libraries that can be linked into multiple projects or loaded dynamically as shared libraries.
  • Build engineers: automate template compilation through CMake helpers so headers regenerate automatically whenever the .htcpp file changes.

How does hypertextcpp work?

You write an .htcpp file with HTML and embedded C++ elements, then run the hypertextcpp command-line tool to transpile it into C++ code. The generated renderer is included in your project and called with a config object containing the data to render; CMake integration functions trigger regeneration during the build.

FAQ

Is hypertextcpp free?

Yes, hypertextcpp is open-source and licensed under the MS-PL license.

What C++ standard does it require?

The generated code and examples use C++17; the CMake example sets target_compile_features(... PUBLIC cxx_std_17).

Can templates be updated without restarting the application?

Yes, using the shared library generation mode, a template library can be rebuilt and reloaded dynamically, though this mode sacrifices compile-time type safety because the config structure must be duplicated and runtime errors abort the program on mismatch.

What output formats are supported?

Three: single header, header and source pair, and shared library source; the header-only mode is the simplest to integrate.