KaisarCode

tpl.c

tpl.c - Template Renderer

tpl.c is a string-based template renderer. It reads a template from standard input, resolves {{}} expressions against injected variables, and writes the rendered output to standard output.

The engine supports includes, scoped variables, named blocks, conditionals, foreach loops, dot-notation aliases, and raw unescaped output. It is designed as a composable native primitive for the KaisarCode ecosystem.


CLI

The tpl command reads a template from standard input. Variables are injected with --var, and the include base directory is set with --root.

Examples

Render a template with inline variables:

echo 'Hello {{name}}' | ./bin/x86_64/linux/tpl --var name=World

Render using a template file:

cat src/tpl/page.html | ./bin/x86_64/linux/tpl --root src --var title=Home

Use raw unescaped output for HTML content:

{{{body}}}

Include reusable templates:

{{@include "tpl/header.html"}}

Parameters

Command/FlagDescription
--root <dir>Base directory for include resolution (default: cwd)
--var <key=value>Inject template variable (repeatable)
-h, --helpShow help and usage
-v, --versionShow version

Template Syntax

All directives use {{}} delimiters.

SyntaxDescription
{{ expr }}HTML-escaped variable or expression
{{{ expr }}}Raw unescaped output
{{/* comment */}}Comment, stripped from output
{{@include "path"}}Include file relative to --root
{{@var name expr}}Set variable in current scope
{{@setblock name}}...{{@endsetblock}}Store body as named block
{{@block name}}Render a named block
{{@block name {"key":"val"}}}Render block with inline properties
{{@if expr}}...{{@else}}...{{@endif}}Conditional branching
{{@foreach item in list}}...{{@endforeach}}Iterate over list

Variables are string-based only. Dot notation (user.name) maps to the underscore-joined flat key (user_name). Lists use comma-separated or [a,b,c] syntax. Truthy values are non-empty strings not equal to "0", "false", or "null".


Public API

#include "tpl.h"

kc_tpl_t *kc_tpl_open(void);
void kc_tpl_close(kc_tpl_t *ctx);
int kc_tpl_set_root(kc_tpl_t *ctx, const char *root);
int kc_tpl_set_var(kc_tpl_t *ctx, const char *key, const char *value);
int kc_tpl_render_string(kc_tpl_t *ctx, const char *input, char **output);
const char *kc_tpl_strerror(const kc_tpl_t *ctx);

Lifecycle

kc_tpl_open()           -- allocate and initialize context
kc_tpl_set_root()       -- configure include base directory
kc_tpl_set_var()        -- inject variables into root scope
kc_tpl_render_string()  -- render template string to output
free(output)            -- caller frees rendered output
kc_tpl_close()          -- release context and all owned data

Build

make clean && make

Compiled artifacts are generated under bin/{arch}/{platform}/ for the host architecture running the build.

Multiarch Builds

make all
make x86_64/linux
make x86_64/windows
make i686/linux
make i686/windows
make aarch64/linux
make aarch64/android
make armv7/linux
make armv7/android
make armv7hf/linux
make riscv64/linux
make powerpc64le/linux
make mips/linux
make mipsel/linux
make mips64el/linux
make s390x/linux
make loongarch64/linux

License

GPLv3

This project is distributed under the GNU General Public License version 3 (GPLv3).


Repo

GitHub: kaisarcode/tpl.c