C++中使用Glaze Stencil/Mustache进行字符串插值
String Interpolation in C++ Using Glaze Stencil/Mustache

原始链接: https://stephenberry.github.io/glaze/stencil-mustache/

Glaze 为 C++ 结构体提供强大的字符串插值功能,支持 stencil 和 mustache 格式,从而实现动态输出生成。Stencil 提供基本的模板功能,包括变量插值、布尔型区块和容器迭代。Mustache 在此基础上增强了 HTML 转义功能,以确保 HTML 输出安全。 主要特性包括: * **变量插值:** 用结构体字段值替换占位符。 * **布尔型区块:** 根据布尔型字段有条件地渲染内容。 * **容器迭代:** 循环遍历结构体内的容器元素。 * **HTML 转义 (Mustache):** 自动转义 HTML 实体,确保输出安全。 Glaze 还提供 `stencilcount` 用于自动文档编号,可以使用 `{{+}}`、`{{++}}` 等创建分层编号方案。错误处理非常健壮,会为缺失键或语法错误等问题提供详细的错误消息。结构体需要使用 Glaze 的反射机制进行反射。

Hacker News new | past | comments | ask | show | jobs | submit login String Interpolation in C++ Using Glaze Stencil/Mustache (stephenberry.github.io) 27 points by npalli 1 day ago | hide | past | favorite | 1 comment jorkingit 22 hours ago [–] I was wondering how they managed to reflect the names of struct fields: looks like https://stackoverflow.com/a/77464529 explains the general idea. Where there's a will, I guess... reply Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4 Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact Search:
相关文章

原文

Glaze provides string interpolation for C++ structs through the stencil and mustache formats. These provide templating mechanisms for formatting structured data into strings, inspired by the Mustache templating language. This enables the generation of dynamic output by combining predefined templates with C++ structs.

Basic Usage

[!NOTE]

result in these examples is a std::expected<std::string, glz::error_ctx>. Like most functions in Glaze (e.g. glz::write_json) you can also pass in your own string buffer as the last argument, in which case the return type is glz::error_ctx.

Template Syntax Specification

Variable Interpolation

  • {{key}} - Replaces with the value of the specified field from the struct
  • {{{key}}} - Triple braces for unescaped output (mustache format only)

Boolean Sections

  • {{#boolean_key}} CONTENT {{/boolean_key}} - Shows CONTENT if the boolean field is true
  • {{^boolean_key}} CONTENT {{/boolean_key}} - Shows CONTENT if the boolean field is false (inverted section)

Container Iteration

  • {{#container_key}} TEMPLATE {{/container_key}} - Iterates over container elements, applying TEMPLATE to each item
  • {{^container_key}} CONTENT {{/container_key}} - Shows CONTENT if the container is empty
  • {{! This is a comment}} - Comments are ignored during template processing

Examples

Boolean Sections

Inverted Sections

Container Iteration

Nested Sections

Empty Container Handling

Mustache Format

Glaze provides glz::mustache with the same interface as glz::stencil, but with HTML escaping behavior:

  • Double braces {{key}} - HTML-escaped output (safe for HTML)
  • Triple braces {{{key}}} - Unescaped output (raw HTML)

HTML Escaping Examples

HTML Entities Escaped

The following characters are escaped in mustache double-brace interpolation: - <&lt; - >&gt; - &&amp; - "&quot; - '&#x27;

Advanced Features

Complex Template Example

Error Handling

The stencil/mustache functions return std::expected<std::string, glz::error_ctx>. Common errors include:

  • glz::error_code::unknown_key - Referenced field doesn't exist in struct
  • glz::error_code::unexpected_end - Mismatched or missing closing tags
  • glz::error_code::syntax_error - Malformed template syntax

[!TIP]

Use glz::format_error like the rest of Glaze to generate a nicely formatted error with context. Note that rather than passing the buffer into the formatter, pass in the layout/template string.

In this example error_msg will look like:

StencilCount

Glaze also provides glz::stencilcount for automatic numbering in documents:

StencilCount Syntax

  • {{+}} - Major section number (1, 2, 3, ...)
  • {{++}} - Sub-section number (1.1, 1.2, 2.1, ...)
  • {{+++}} - Sub-sub-section number (1.1.1, 1.1.2, ...)
  • And so on for deeper nesting levels

Requirements

  • Structs must be reflectable (using Glaze reflection) or be glaze objects
  • Boolean fields are used for section conditions
  • All field names in templates must exist in the struct
联系我们 contact @ memedata.com