| name | websun |
| description | Websun HTML template parser syntax reference. Use when editing or creating .tpl template files, or when working with websun.php. Covers variables, conditions, cycles, nested templates, and function calls. |
Websun HTML template parser
Condensed syntax reference (skill) for AI assistants.
Templates use the .tpl extension (also allowed: .html, .css, .js, .xml).
Quick syntax reference
Variables
| Syntax | Meaning |
|---|
{*var*} | Variable value |
{*a.b.c*} | Nested access via dot (outside cycles) |
{*a:b*} | Nested access via colon (inside cycles, a is the iterated array) |
{*$_GET.foo*} | Global / superglobal variable ($ prefix) |
{*=CONST_NAME*} | PHP constant |
{*"text"*} | String literal |
{*123*} | Numeric literal |
{*TRUE*} / {*FALSE*} | Boolean |
{*var^COUNT*} | count($var) |
{*var1|var2|"default"*} | First non-empty alternative |
{**} | Root variable passed to template |
Missing variables silently resolve to empty string — no errors or warnings.
Comments
/* C-style block comments */
Conditions (if)
| Syntax | Meaning |
|---|
{?*var*}...{?} | If var is truthy |
{?!*var*}...{?} | If var is falsy (negated) |
{?*var*}yes{?!}no{?} | If/else |
{?*a = b*}...{?} | Equal (loose) |
{?*a == b*}...{?} | Equal (strict) |
{?*a != b*}...{?} | Not equal (loose) |
{?*a !== b*}...{?} | Not equal (strict) |
{?*a > b*}...{?} | Greater than |
{?*a >= b*}...{?} | Greater or equal |
{?*a < b*}...{?} | Less than |
{?*a <= b*}...{?} | Less or equal |
{?*c1|c2*}...{?} | OR conditions |
{?*c1&c2*}...{?} | AND conditions |
Right side of comparison: variable name by default; "quoted string" for string; digits for number.
Functions in conditions: {?* @in_array(10, *a*) *}...{?}
Conditions can be nested.
Cycles (loops)
| Syntax | Meaning |
|---|
{%*array*}...{%} | Iterate over array |
{%*array*}body{%!}empty{%} | With negative part (shown if array empty) |
{%**}...{%} | Iterate over root array |
Inside a cycle, access current element's keys with colon notation:
{%*menu*}
<a href="{*menu:url*}">{*menu:title*}</a>
{%}
Special iteration variables (suffix after array:):
| Syntax | Meaning |
|---|
{*array:^KEY*} | Current key (string or numeric) |
{*array:^i*} | Zero-based index (0, 1, 2, ...) |
{*array:^N*} | One-based number (1, 2, 3, ...) |
Cycles can be nested to any depth.
Nested templates (includes)
| Syntax | Meaning |
|---|
{* + tpl.tpl *} | Include template with all current data |
{* + *var* | tpl.tpl *} | Include template with var as root data |
{* + *%var* | tpl.tpl *} | Iterate over var, include template per element |
{* + *?var* | tpl.tpl *} | Include only if var is truthy |
{* + *tpl_var* *} | Template path from variable |
{* + *var* | >*code_var* *} | Inline template from variable |
{* + ^T *} | Recursive call to current template |
Inside an included template that received a partial data slice, use {*:key*} or just {*key*} to access the passed element's fields.
Template path prefixes
| Prefix | Resolves relative to |
|---|
(none) tpl.tpl | Current template's directory |
^/tpl.tpl | Templates root dir (passed as 3rd arg to websun_parse_template_path) |
$/path/tpl.tpl | $_SERVER['DOCUMENT_ROOT'] |
/abs/path/tpl.tpl | Filesystem absolute path |
Function calls
| Syntax | Meaning |
|---|
{* @func(args) *} | Call function, insert result |
{* @func(args) | tpl.tpl *} | Call function, pass result as data to template |
{* @func( @other() ) *} | Nested function calls |
{* @*obj*->method(args) *} | Object method (obj from variable) |
{* Class::method(args) *} | Static method |
{* @*func_var*(args) *} | Function name from variable |
Function arguments accept: variables *var*, strings "text", numbers 123, JSON arrays [1,2,3], JSON objects {"a":"b"}.
Allowed callbacks (default): array_key_exists, date, DateTime::format, htmlspecialchars, implode, in_array, is_array, is_null, json_encode, mb_lcfirst, mb_ucfirst, rand, round, strftime, urldecode, var_dump.
Register more via global $WEBSUN_ALLOWED_CALLBACKS array.
PHP entry points
$html = websun_parse_template_path($DATA, $tpl_path, $templates_root_dir);
$html = websun_parse_template($DATA, $template_code, $templates_root_dir);
Set ini_set('pcre.backtrack_limit', 1024*1024) for large templates.