Variables
Use template variables so the same template can generate images with different content
Variables turn an imgrender template from fixed content into a reusable template. You can insert variable placeholders in HTML or CSS, then replace them with specific values at render time.
<div style="background-color: {$ theme_color $}">
<h1>{$ title $}</h1>
<p>{$ description | default("No description") $}</p>
</div>This template parameterizes the color and the text with three variables. Replace them with real values at render time to generate different images:
{$ theme_color $}is written in the HTMLstyleattribute and becomes the background color after replacement (for example,#1677FF){$ title $}is output directly as a child node for the title text{$ description | default("No description") $}applies a filter with|after the variable: when the value is omitted or empty, it outputs "No description"
A variable placeholder uses {$ variable_name $} and can be written directly in HTML text or attribute values.
Variable syntax
imgrender marks variables with placeholders wrapped in { and $. The format is:
{$ variable_name $}You can use them in both HTML and CSS:
<div style="color: {$ brand_color $}; font-size: 32px">{$ title $}</div>.banner {
background-image: url({$ cover_url $});
color: {$ brand_color $};
}Variable name rules
A variable name must be a valid identifier:
- Starts with a letter or an underscore
- Contains only letters, digits, and underscores
- Chinese characters, hyphens, spaces, and a leading digit are not supported
✅ title _id brand_color2
❌ 标题 pod-title 2title pod titleTo access a field on an object, use a dot path:
{$ user.name $}
{$ product.price $}Data types
Each variable has a specific data type. The template editor renders the matching input for that type, and the API runs basic validation against the type.
| Type | Description | Example |
|---|---|---|
string | Text | Major release |
number | Number | 42 |
boolean | Boolean | true / false |
color | Color; hex format recommended | #1677FF |
image_url | Image URL | https://example.com/cover.jpg |
date | Date string | 2026-08-23 |
array | Array | ["a", "b", "c"] |
object | Object | { "name": "imgrender" } |
Where each type is used
- string / number: Text, prices, quantities, and similar values
- boolean: Use with
{% if %}to control whether an element is shown - color: CSS values such as background color, text color, and border color
- image_url: An
<img>src, or a CSSbackground-image - date: A date that is formatted before it is shown
- array / object: Use with a
{% for %}loop, or to access object properties
Defaults and required values
You can configure each variable with:
- Required: When enabled, an API call must provide a valid value for this variable, or validation fails
- Default: The fallback when the variable is omitted or empty
- Sample value: The value the template editor uses for live preview
<h1>{$ title $}</h1>
<p>{$ description | default("No description") $}</p>If description is an empty string or is not set, the default filter outputs "No description".
Sample value vs default
- Sample value only affects the template editor preview and the generated sample API request
- Default applies during server-side rendering, when the request omits the variable or the value is empty
Text filters
Append a filter after a variable with | to transform the output. The following filters are supported:
upper— Convert to uppercase{$ title | upper $}→HELLO
lower— Convert to lowercase{$ title | lower $}→hello
capitalize— Capitalize the first letter{$ title | capitalize $}→Hello
trim— Trim leading and trailing whitespace{$ title | trim $}→hello
truncate(n)— Truncate to n characters{$ title | truncate(5) $}→hello…
replace(a, b)— Replace text{$ title | replace("o", "a") $}→hella
default(v)— Return a default when the value is empty{$ title | default("Untitled") $}→Untitled
Filters can be chained:
<p>{$ title | trim | upper | truncate(10) $}</p>Conditional rendering
Use {% if %} to control whether a block of content is output:
{% if show_badge %}
<span style="background: red; color: white">NEW</span>
{% endif %}{% else %} and {% elif %} are also supported:
{% if is_vip %}
<span>VIP member</span>
{% elif is_paid %}
<span>Paid member</span>
{% else %}
<span>Free user</span>
{% endif %}A condition can use the not prefix:
{% if not hide_footer %}
<footer>{$ footer_text $}</footer>
{% endif %}Condition rules
imgrender evaluates conditions with truthy semantics: an empty string, 0, false, null, and undefined are falsy. Boolean variables, non-empty strings, and non-zero numbers are truthy.
Loop rendering
Loop over an array variable with {% for %}:
<ul>
{% for tag in tags %}
<li>{$ tag $}</li>
{% endfor %}
</ul>Variable values to pass:
{
"tags": ["React", "Vue", "Svelte"]
}Rendered output:
<ul>
<li>React</li>
<li>Vue</li>
<li>Svelte</li>
</ul>The loop variable must be a valid identifier. The array itself can be an array of objects, and you can read fields with dot access:
{% for member in team %}
<div>{$ member.name $} - {$ member.role $}</div>
{% endfor %}Managing variables in the template editor
The Variables panel in the template editor extracts variable names from the HTML and CSS automatically.
You can:
- Click a variable card to expand it and edit the type, default, sample value, and description
- Set whether the variable is required
- After you remove a variable reference from the code, that variable is shown as Unused. You can keep the configuration or remove it
Automatic extraction
The editor extracts variable names automatically from { $ ... $ } and {% %} in the code, so you do not need to create variables by hand. Write the placeholders in the template.
Passing variables through the API
After you save it as a template, call the Generate image from template API and pass variable values in the variables field of the request body:
curl -X POST https://api.imgrender.com/open/v1/images/templates/:template_id/render \
-H "Content-Type: application/json" \
-H "X-API-Key: YOUR_API_KEY" \
-d '{
"variables": {
"title": "Weekend giveaway",
"theme_color": "#FF6B35",
"show_badge": true,
"tags": ["Perks", "Limited time"]
}
}'The API validates the request against the variable types defined on the template. If validation fails, it returns 400 and a specific error message.
Full example
<div
style="width: 100%; height: 100%; display: flex; flex-direction: column; justify-content: center; padding: 48px; background-color: {$ background_color $}"
>
<h1 style="font-size: 48px; color: #1a1a1a; margin: 0">
{$ title $}
</h1>
<p style="font-size: 22px; color: #555555; margin-top: 16px">
{$ description | default("No description") $}
</p>
{% if show_tags %}
<div style="display: flex; gap: 8px; margin-top: 24px">
{% for tag in tags %}
<span
style="padding: 6px 12px; border-radius: 999px; background-color: #ffffff; color: {$ background_color $}"
>
{$ tag $}
</span>
{% endfor %}
</div>
{% endif %}
</div>Example variable configuration:
[
{
"name": "title",
"type": "string",
"required": true,
"sampleValue": "Major release"
},
{
"name": "description",
"type": "string",
"required": false,
"defaultValue": ""
},
{
"name": "background_color",
"type": "color",
"required": false,
"defaultValue": "#1677FF"
},
{
"name": "show_tags",
"type": "boolean",
"required": false,
"defaultValue": true
},
{
"name": "tags",
"type": "array",
"required": false,
"defaultValue": []
}
]Next steps
- Open the default template in the template editor, change the variable values, and watch the live preview
- See the Generate image from template API to connect the template to your application
- See Layout and Style to control how the template looks