Templating

The template engine processes ${variable} placeholders and foreach loops inside the document tree, enabling data-driven document generation.

DocraftTemplateEngine

class DocraftTemplateEngine

The DocraftTemplateEngine class is responsible for processing templates and generating the final document output.

It takes a parsed document tree and applies template logic to produce the rendered output.

Public Functions

DocraftTemplateEngine() = default
void template_nodes(const std::vector<std::shared_ptr<model::DocraftNode>> &nodes)

Renders the document using the provided template variables.

Returns:

Rendered document as a string.

void add_template_variable(const std::string &name, const std::string &value)

Adds a template variable.

Parameters:
  • name – Variable name (case-insensitive).

  • value – Variable value inserted into templates.

std::string get_template_variable(const std::string &name)

Retrieves a template variable value.

Parameters:

name – Variable name (case-insensitive).

Throws:

std::runtime_error – if not found.

Returns:

Stored value.

void clear_template_variables()

Clears all template variables.

void remove_template_variable(const std::string &name)

Removes a template variable by name.

Parameters:

name – Variable name (case-insensitive).

Throws:

std::runtime_error – if not found.

int items() const

Returns the number of stored template variables.

bool has_template_variable(const std::string &name) const

Checks if a template variable exists.

Parameters:

name – Variable name (case-insensitive).

void add_image_data(const std::string &image_id, const std::vector<unsigned char> &data, int width, int height)

Adds raw RGB image data.

Parameters:
  • image_id – Image id used by <Image data=”…”> in templates (case-insensitive).

  • data – Raw RGB bytes (3 bytes per pixel, row-major).

  • width – Pixel width.

  • height – Pixel height.

void add_base64_image_data(const std::string &image_id, std::string_view base64, int width, int height)

Adds raw RGB image data from a base64 string.

Parameters:
  • image_id – Image id used by <Image data=”…”> in templates (case-insensitive).

  • base64 – Base64 string with raw RGB bytes (no data URI prefix).

  • width – Pixel width.

  • height – Pixel height.

Throws:

std::runtime_error – if decoded size does not match width*height*3.

const RawImageData &get_image_data(const std::string &image_id) const

Retrieves raw image data by id.

Parameters:

image_id – Image id (case-insensitive).

Throws:

std::runtime_error – if not found.

Returns:

RawImageData reference.

struct RawImageData

Struct to hold raw image data along with its dimensions.

Public Members

std::vector<unsigned char> data
int width = 0
int height = 0