KaisarCode

tpl.c

tpl.c - Template Renderer

tpl.c is a simple template renderer with includes, scoped variables, blocks, and basic control directives. It provides libtpl for C callers and a tpl CLI that reads the template from standard input and writes the rendered output to standard output.


CLI

Examples

Render a template with variables:

echo '<h1>{{ title }}</h1>' | ./bin/x86_64/linux/tpl --var title=Home

Render with raw (unescaped) output:

echo '{{{ body }}}' | ./bin/x86_64/linux/tpl --var body='<b>bold</b>'

Render a file with includes:

./bin/x86_64/linux/tpl --root ./views --var page=Home < views/page.html

Parameters

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

Template Syntax

DirectiveDescription
{{ expr }}HTML-escaped output
{{{ expr }}}Raw (unescaped) output
{{ object.field }}Dot notation over flat object_field keys and foreach aliases
{{/* comment */}}Template comment, stripped from output
{{@include "path"}}Include a file relative to --root
{{@var name expr}}Set a variable in the current scope
{{@setblock name}} ... {{@endsetblock}}Define a named block
{{@block name}}Render a named block
{{@block name { key: val }}}Render a block with inline props
{{@if expr}} ... {{@else}} ... {{@endif}}Conditional rendering
{{@foreach item in list}} ... {{@endforeach}}Iterate over a comma-separated list or [a,b,c]

Variables are string-based. Lists are passed as CSV or [a,b,c]. Truthy values are non-empty strings except 0, false, and null. Directives inside HTML comments (<!-- -->) are not evaluated.


Public API

#include "tpl.h"

kc_tpl_t *ctx = kc_tpl_open();
char *output = NULL;

kc_tpl_set_root(ctx, ".");
kc_tpl_set_var(ctx, "title", "Home");
kc_tpl_render_string(ctx, "<h1>{{ title }}</h1>", &output);

free(output);
kc_tpl_close(ctx);

Lifecycle

  • kc_tpl_open() - allocates and returns a new renderer context owned by the caller.
  • kc_tpl_set_root() - configures include path resolution for the context.
  • kc_tpl_set_var() - stores string variables in the context scope.
  • kc_tpl_render_string() - renders one template into a caller-owned output buffer.
  • kc_tpl_close() - releases the context and all associated variable storage.

Build

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

make clean && make

Multiarch Builds

The project is prepared to build artifacts for multiple architectures under bin/{arch}/{platform}/. A plain make builds only the current host architecture, while the targets below build the full matrix or a specific target.

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

Beta Notice

This is a beta project tested only on Debian x86_64. It was created out of a personal need for these libraries, but no guarantees are provided regarding its stability or future support. You are free to test it, use it, and modify it as you please.

If you'd like to reach out, you can send an email to [email protected]. Please note that I do not accept pull requests; the goal is to avoid long-term dependency on platforms like GitHub, and I do not maintain fixed infrastructure to guarantee long-term stability for these projects.


Repo

You can download the repository and read the most up-to-date documentation directly from its official source.

GitHub: kaisarcode/tpl.c

License

GPLv3

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