Rendering text templates with the CWIST template engine declared in
include/cwist/core/template/template.h. A single cwist_template_render
call takes a template string and a cJSON context and returns the filled
result as a cwist_sstring.
| Step | Shows |
|---|---|
step-1-render |
variable substitution with {{ name }}, conditional blocks with {% if %}, and loops over arrays with {% for %} |
Build the library first, from the repository root:
makeThen build the example (it ships its own Makefile):
make -C example/template/step-1-rendermake examples-check also builds it.
./example/template/step-1-render/step-1-renderThe program renders three templates against three cJSON contexts:
- Variable substitution —
{{ name }}and{{ version }}are replaced by the matching context fields. - Conditional block —
{% if logged_in %}...{% endif %}is emitted when the field is true; the{% if guest %}block is skipped because the context has noguestfield. - Loop over array —
{% for fruit in fruits %}repeats its body for each element and resolves{{ fruit.name }}inside the loop.
Example output:
=== Template Rendering Tutorial ===
[Variable substitution]
Output: Hello, CWIST! This is version 1.
[Conditional block]
Output: Welcome back, Alice!
[Loop over array]
Output:
Fruit list:
- Apple
- Banana
- Cherry
=== Done ===
make -C example/template/step-1-render clean