Skip to content

<var>

A tag that sets a dependency value. The value is stored in the current Dependencies instance using the specified name as the key.

When a parent element contains <var> children and no explicit dependenciesScope is set, XWidget automatically uses copy scoping to prevent variables from leaking into the parent's dependencies.

Attributes

Name Type Description Required Default
name String The dependency key to store the value. Use ... for the spread operator. Yes -
value dynamic The value to store. Supports EL expressions. The value can be null. Yes -

Examples

Basic Variable

<var name="greeting" value="Hello World"/>
<Text data="${greeting}"/>

Variable from Expression

<var name="fullName" value="${user.firstName + ' ' + user.lastName}"/>
<Text data="${fullName}"/>

Spread Operator

Use ... as the name to merge a map into the current dependencies:

<var name="..." value="${userData}"/>
<Text data="${firstName}"/>
<Text data="${lastName}"/>

This spreads all entries from userData into the dependency scope, making them accessible as individual keys. The value must be a Map<String, dynamic>.

Setting Computed Values

<var name="itemCount" value="${length(items)}"/>
<var name="hasItems" value="${itemCount > 0}"/>
<if test="${hasItems}">
<Text data="Found ${toString(itemCount)} items"/>
</if>

Using with Fragments

Variables set before a <fragment> tag are available inside the fragment:

<var name="showHeader" value="${true}"/>
<var name="title" value="Settings"/>
<fragment name="page_layout"/>

See Also

  • Dependencies - Dependency system details
  • <fragment> - For including fragments
  • <forEach> - For iterating with loop variables