template.h reference

Module <cweb/template.h> — template output and buffer

Global accumulation of HTML/text output from generated templates plus helpers for asset URLs.

Type cweb_buffer_t

  • data — buffer pointer.
  • size — current length.
  • capacity — reserved capacity.

Global variable

extern cweb_buffer_t g_output_buffer

  • Purpose: Default target for template output (build/runtime of template engine).

Global output API

void cweb_output_init(void)

  • Purpose: Initializes global output buffer.

void cweb_output_raw(const char *str)

  • Purpose: Appends raw text without HTML escaping.

void cweb_output_html(const char *str)

  • Purpose: Escapes text via cweb_escape_html and appends to global buffer (safe for dynamic content).

char *cweb_output_get(void)

  • Purpose: Returns a copy of current buffer content (malloc + strcpy).
  • Memory: caller must free return value; global buffer unchanged until cweb_output_cleanup.

void cweb_output_cleanup(void)

  • Purpose: Frees global buffer and resets state.

Local buffers

void cweb_buffer_init(cweb_buffer_t *buffer)

  • Purpose: Initializes a standalone cweb_buffer_t.

void cweb_buffer_append(cweb_buffer_t *buffer, const char *str)

  • Purpose: Appends string to buffer.

void cweb_buffer_cleanup(cweb_buffer_t *buffer)

  • Purpose: Frees buffer->data.

Asset links

bool cweb_setAssetLink(char **html, const char *logical_name, const char *resolved_path, const char *rel_attr)

  • Purpose: Replaces or injects a link to an asset (<link> / <script> / similar) from a logical name and resolved URL.
  • Parameters:
    • html — pointer to HTML string (may be reallocated).
    • logical_name — e.g. "styles.css".
    • resolved_path — public path, e.g. "/assets/styles.css".
    • rel_attr — optional rel attribute or similar (semantics per implementation).
  • Return: true on success, else false.

See also