# Template Syntax SUI uses a simple template syntax for data binding, conditional rendering, and list iteration. ## Data Interpolation Use double curly braces `{{ }}` to output data: ```html {{ name }} {{ user.email }} {{ items[0].title }} {{ title ?? 'Default Title' }} {{ user.name ?? 'Anonymous' }} {{ price * quantity }} {{ firstName + ' ' + lastName }} {{ count > 0 ? 'Has items' : 'Empty' }} ``` ## Conditional Rendering ### Basic If ```html
Active
Has items
Logged in
``` ### If-Elif-Else ```html
Active
Pending
Suspended
Unknown
``` ### Comparison Operators | Operator | Description | | -------- | --------------------- | | `==` | Equal | | `!=` | Not equal | | `>` | Greater than | | `<` | Less than | | `>=` | Greater than or equal | | `<=` | Less than or equal | | `&&` | Logical AND | | `\|\|` | Logical OR | | `!` | Logical NOT | ### Examples ```html
Admin Panel
Access Granted
Content loaded
Verified User
``` ## List Rendering ### Basic Loop ```html ``` ### With Index ```html ``` ### Nested Loops ```html

{{ category.name }}

``` ### Loop with Conditional ```html
{{ user.name }}
``` ### Object Iteration ```html
{{ key }}
{{ value }}
``` ## Variable Assignment Use `` to define variables: ```html Total: {{ total }} ``` ## Attribute Binding ### Dynamic Attributes ```html Profile {{ imageAlt }} ``` ### Conditional Attributes ```html
Panel
Content
``` ### Spread Attributes ```html
``` ## Raw HTML Output By default, output is HTML-escaped. Use `s:raw` for raw HTML: ```html
{{ htmlContent }}
{{ htmlContent }}
``` ## Expression Engine SUI uses [Expr](https://expr-lang.org/) (v1.17) as the expression engine. Expr provides a powerful expression language with operators, functions, and more. ### SUI Custom Functions | Function | Description | Example | | --------------- | ------------------------------ | --------------------------------- | | `P_(proc, ...)` | Call a Yao process | `{{ P_('models.user.Find', 1) }}` | | `True(value)` | Check if value is truthy | `{{ True(user) }}` | | `False(value)` | Check if value is falsy | `{{ False(error) }}` | | `Empty(value)` | Check if array/object is empty | `{{ Empty(items) }}` | ### Expr Built-in Functions Expr provides many built-in functions. Here are commonly used ones: | Function | Description | Example | | --------------------- | ------------------------------ | --------------------------------- | | `len(array)` | Get length of array/string/map | `{{ len(items) }}` | | `all(array, pred)` | Check if all elements match | `{{ all(users, .active) }}` | | `any(array, pred)` | Check if any element matches | `{{ any(items, .price > 100) }}` | | `one(array, pred)` | Check if exactly one matches | `{{ one(users, .admin) }}` | | `none(array, pred)` | Check if no elements match | `{{ none(items, .deleted) }}` | | `map(array, mapper)` | Transform array elements | `{{ map(users, .name) }}` | | `filter(array, pred)` | Filter array by predicate | `{{ filter(items, .active) }}` | | `find(array, pred)` | Find first matching element | `{{ find(users, .id == 1) }}` | | `count(array, pred)` | Count matching elements | `{{ count(items, .price > 50) }}` | | `sum(array)` | Sum of array elements | `{{ sum(prices) }}` | | `mean(array)` | Average of array elements | `{{ mean(scores) }}` | | `min(array)` | Minimum value | `{{ min(prices) }}` | | `max(array)` | Maximum value | `{{ max(scores) }}` | | `first(array)` | First element | `{{ first(items) }}` | | `last(array)` | Last element | `{{ last(items) }}` | | `take(array, n)` | Take first n elements | `{{ take(items, 5) }}` | | `keys(map)` | Get map keys | `{{ keys(settings) }}` | | `values(map)` | Get map values | `{{ values(settings) }}` | | `contains(a, b)` | Check if a contains b | `{{ contains(name, 'test') }}` | | `startsWith(s, pre)` | Check string prefix | `{{ startsWith(url, 'https') }}` | | `endsWith(s, suf)` | Check string suffix | `{{ endsWith(file, '.pdf') }}` | | `upper(s)` | Uppercase string | `{{ upper(name) }}` | | `lower(s)` | Lowercase string | `{{ lower(email) }}` | | `trim(s)` | Trim whitespace | `{{ trim(input) }}` | | `split(s, sep)` | Split string | `{{ split(tags, ',') }}` | | `join(array, sep)` | Join array to string | `{{ join(names, ', ') }}` | | `int(v)` | Convert to integer | `{{ int(value) }}` | | `float(v)` | Convert to float | `{{ float(value) }}` | | `string(v)` | Convert to string | `{{ string(count) }}` | | `now()` | Current time | `{{ now() }}` | | `date(s)` | Parse date string | `{{ date('2024-01-01') }}` | | `duration(s)` | Parse duration string | `{{ duration('1h30m') }}` | For the complete list of built-in functions and operators, see the [Expr Language Definition](https://expr-lang.org/docs/language-definition). ### Examples ```html
No users found
{{ P_('utils.formatDate', createdAt) }} Total: {{ len(items) }} items Active: {{ count(users, .active) }} Sum: {{ sum(map(items, .price)) }} {{ upper(first(split(name, ' '))) }}
{{ item.name }}
``` ## String Operations ```html {{ 'Hello, ' + name + '!' }} Edit ``` ## Arithmetic Operations ```html {{ price * quantity }} {{ total / count }} {{ value + 10 }} {{ index - 1 }} {{ (completed / total) * 100 }}% ``` ## Comments HTML comments are preserved in output: ```html ``` ## Whitespace Control SUI preserves whitespace by default. For minified output, use build options: ```bash yao sui build