Implementation:ClickHouse ClickHouse Poco JSON Template
| Knowledge Sources | |
|---|---|
| Domains | JSON, Templating |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
A JSON templating engine that supports conditional logic, loops, and template inclusion for dynamic content generation.
Description
The Poco JSON `Template` class provides a templating system that processes JSON data with embedded template commands. It supports control structures like conditionals (`if`, `elsif`, `else`), loops (`for`), variable expansion (`echo`), and template inclusion (`include`). Templates are parsed from files or streams and rendered with JSON data to produce output.
The implementation uses a part-based architecture where templates are composed of parts: `StringPart` for literal text, `EchoPart` for variable expansion, `LogicPart` for conditionals, `LoopPart` for iterations, and `IncludePart` for nested templates.
Usage
ClickHouse vendors this Poco component to support JSON-based templating scenarios where dynamic content generation from structured data is needed. This can be used for report generation, configuration file templating, or any scenario requiring data-driven text output.
Code Reference
Source Location
- Repository: ClickHouse
- File: base/poco/JSON/src/Template.cpp
- Lines: 1-701
Signature
class Template {
public:
Template(const Path& templatePath);
Template();
~Template();
void parse();
void parse(std::istream& in);
void render(const Var& data, std::ostream& out) const;
};
// Internal part hierarchy
class Part {
virtual void render(const Var& data, std::ostream& out) const = 0;
};
class EchoPart : public Part {
EchoPart(const std::string& query);
};
class LogicPart : public MultiPart {
void addPart(LogicQuery* query, Part* part);
};
class LoopPart : public MultiPart {
LoopPart(const std::string& name, const std::string& query);
};
Import
#include <Poco/JSON/Template.h>
I/O Contract
| Input | Output |
|---|---|
| Template file path or stream with embedded commands | Parsed template structure ready for rendering |
| JSON data (`Poco::Dynamic::Var`) | Rendered text output to stream |
Template Commands
| Command | Syntax | Description |
|---|---|---|
| echo | `<?= query ?>` or `<? echo query ?>` | Outputs the value of a JSON query |
| if | `<? if query ?>...<?endif?>` | Conditional block based on query truthiness |
| ifexist | `<? ifexist query ?>...<?endif?>` | Conditional block based on query existence |
| elsif/elif | `<? elsif query ?>` | Alternative conditional branch |
| else | `<? else ?>` | Default conditional branch |
| for | `<? for var query ?>...<?endfor?>` | Iterates over array from query |
| include | `<? include "path" ?>` | Includes another template file |
Usage Examples
// Parse a template from file
Template tpl("/path/to/template.tpl");
tpl.parse();
// Create JSON data
Object::Ptr data = new Object();
data->set("title", "Report");
data->set("count", 42);
Array::Ptr items = new Array();
items->add("Item 1");
items->add("Item 2");
data->set("items", items);
// Render template with data
std::ostringstream oss;
tpl.render(data, oss);
std::string result = oss.str();
// Template example:
// <h1><?= title ?></h1>
// <? if count ?>Count: <?= count ?><?endif?>
// <? for item items ?>
// <li><?= item ?></li>
// <?endfor?>