# Thymeleaf Attribute Reference

## Standard Attributes

### Text and Content Attributes

| Attribute  | Purpose                     | Example                                 |
| ---------- | --------------------------- | --------------------------------------- |
| `th:text`  | Sets text content (escaped) | `<p th:text="${message}">Default</p>`   |
| `th:utext` | Sets unescaped text content | `<div th:utext="${html}">Default</div>` |
| `th:value` | Sets element value          | `<input th:value="${user.name}" />`     |
| `th:src`   | Sets source attribute       | `<img th:src="@{/images/logo.png}" />`  |
| `th:href`  | Sets href attribute         | `<a th:href="@{/users/${id}}">Link</a>` |

### Conditional Attributes

| Attribute   | Purpose               | Example                                         |
| ----------- | --------------------- | ----------------------------------------------- |
| `th:if`     | Conditional rendering | `<div th:if="${user.isAdmin()}">Admin</div>`    |
| `th:unless` | Negated condition     | `<div th:unless="${user.isGuest()}">User</div>` |
| `th:switch` | Switch statement      | `<div th:switch="${user.role}">...</div>`       |
| `th:case`   | Case in switch        | `<p th:case="'ADMIN'">Admin</p>`                |

### Iteration Attributes

| Attribute | Purpose                   | Example                                      |
| --------- | ------------------------- | -------------------------------------------- |
| `th:each` | Loop over collections     | `<tr th:each="item : ${items}">...</tr>`     |
| `th:with` | Local variable definition | `<div th:with="name=${user.name}">...</div>` |

### Form Attributes

| Attribute   | Purpose             | Example                                       |
| ----------- | ------------------- | --------------------------------------------- |
| `th:object` | Form backing object | `<form th:object="${user}">...</form>`        |
| `th:field`  | Field binding       | `<input th:field="*{name}" />`                |
| `th:action` | Form action URL     | `<form th:action="@{/users/save}">...</form>` |
| `th:method` | HTTP method         | `<form th:method="post">...</form>`           |

### Fragment Attributes

| Attribute     | Purpose               | Example                                                |
| ------------- | --------------------- | ------------------------------------------------------ |
| `th:fragment` | Define fragment       | `<div th:fragment="header">...</div>`                  |
| `th:insert`   | Insert fragment       | `<div th:insert="~{frag/header :: header}">...</div>`  |
| `th:replace`  | Replace with fragment | `<div th:replace="~{frag/header :: header}">...</div>` |
| `th:include`  | Include fragment      | `<div th:include="~{frag/header :: header}">...</div>` |

### Link and URL Attributes

| Attribute   | Purpose        | Example                                  |
| ----------- | -------------- | ---------------------------------------- |
| `th:href`   | URL generation | `<a th:href="@{/users/${id}}">User</a>`  |
| `th:src`    | Resource URL   | `<img th:src="@{/images/pic.jpg}" />`    |
| `th:action` | Form action    | `<form th:action="@{/login}">...</form>` |

### Utility Attributes

| Attribute        | Purpose                 | Example                                                      |
| ---------------- | ----------------------- | ------------------------------------------------------------ |
| `th:attr`        | Set multiple attributes | `<input th:attr="data-id=${user.id}, class=${user.role}" />` |
| `th:attrappend`  | Append attributes       | `<div th:attrappend="class=${user.role}">...</div>`          |
| `th:classappend` | Append CSS class        | `<div th:classappend="${user.role}">...</div>`               |
| `th:styleappend` | Append CSS style        | `<div th:styleappend="color: red;">...</div>`                |

### Processing Attributes

| Attribute   | Purpose                         | Example                                       |
| ----------- | ------------------------------- | --------------------------------------------- |
| `th:remove` | Remove element after processing | `<div th:remove="tag">Template comment</div>` |
| `th:nowrap` | Prevent whitespace trimming     | `<span th:nowrap="${user.name}">Name</span>`  |

## Expression Syntax

### Variable Expressions `${...}`

```html
<!-- Simple variables -->
<span th:text="${user.name}">Name</span>

<!-- Property navigation -->
<span th:text="${user.address.city}">City</span>

<!-- Method calls -->
<span th:text="${user.getFullName()}">Full Name</span>

<!-- Map access -->
<span th:text="${config['app.name']}">App Name</span>
```

### Message Expressions `#{...}`

```html
<!-- Simple messages -->
<span th:text="#{welcome.message}">Welcome</span>

<!-- Parameterized messages -->
<span th:text="#{user.greeting(${user.name})}">Hello User</span>

<!-- Default values -->
<span th:text="#{welcome.message, 'Default Welcome'}">Welcome</span>
```

### Link Expressions `@{...}`

```html
<!-- Simple URLs -->
<a th:href="@{/users}">Users</a>

<!-- Path variables -->
<a th:href="@{/users/{id}(id=${user.id})}">User</a>

<!-- Query parameters -->
<a th:href="@{/search(q=${search.term})}">Search</a>

<!-- Multiple parameters -->
<a th:href="@{/users(page=${page}, size=${size})}">Users</a>
```

### Fragment Expressions `~{...}`

```html
<!-- Simple fragment -->
<div th:replace="~{fragments/header :: header}">Header</div>

<!-- Template with parameters -->
<div
  th:replace="~{fragments/pagination :: pagination(page=${page}, total=${total})}"
>
  Pagination
</div>

<!-- No-op fragment (content only) -->
<div th:replace="~{::content}">Content</div>
```

### Literal Expressions

```html
<!-- String literals -->
<span th:text="'Hello, World!'">Hello</span>

<!-- Numeric literals -->
<span th:text="${42}">42</span>

<!-- Boolean literals -->
<div th:if="${true}">Always shown</div>

<!-- Null literal -->
<span th:text="${null}">Empty</span>
```

## Utility Objects Reference

### #strings

```html
<!-- String operations -->
<span th:text="${#strings.toUpperCase(name)}">UPPERCASE</span>
<span th:text="${#strings.substring(email, 0, 3)}">***</span>
<span th:text="${#strings.length(message)}">0</span>

<!-- String checks -->
<div th:if="${#strings.isEmpty(name)}">Empty name</div>
<div th:if="${#strings.contains(name, 'admin')}">Contains admin</div>
<div th:if="${#strings.startsWith(name, 'Mr.')}">Mr. detected</div>

<!-- String manipulation -->
<span th:text="${#strings.replace(text, 'old', 'new')}">Replaced</span>
<span th:text="${#strings.trim(text)}">Trimmed</span>
<span th:text="${#strings.join(list, ', ')}">Joined</span>
```

### #numbers

```html
<!-- Number formatting -->
<span th:text="${#numbers.formatDecimal(price, 2, 2)}">0.00</span>
<span th:text="${#numbers.formatInteger(count)}">0</span>
<span th:text="${#numbers.formatPercent(discount, 1, 1)}">0%</span>

<!-- Number operations -->
<span th:text="${#numbers.sequence(0, 9)}">0-9</span>
<span th:text="${#numbers.random(100)}">Random</span>
```

### #dates

```html
<!-- Date formatting -->
<span th:text="${#dates.format(date, 'yyyy-MM-dd')}">2024-01-01</span>
<span th:text="${#dates.format(timestamp, 'MMM dd, yyyy')}">Jan 01, 2024</span>

<!-- Date creation -->
<span th:text="${#dates.createNow()}">Current Date</span>
<span th:text="${#dates.create(date, 'yyyy-MM-dd')}">Parsed Date</span>

<!-- Date operations -->
<div th:if="${#dates.isAfter(date1, date2)}">Date1 is after Date2</div>
<span th:text="${#dates.month(date)}">1</span>
<span th:text="${#dates.year(date)}">2024</span>
```

### #lists

```html
<!-- List operations -->
<span th:text="${#lists.size(users)}">0</span>
<div th:if="${#lists.isEmpty(items)}">No items</div>

<!-- List manipulation -->
<span th:text="${#lists.contains(roles, 'ADMIN')}">Admin role</span>
<span th:text="${#lists.indexOf(items, 'target')}">Index</span>
<span th:text="${#lists.subList(items, 0, 5)}">First 5</span>
```

### #sets

```html
<!-- Set operations -->
<span th:text="${#sets.size(roles)}">0</span>
<div th:if="${#sets.contains(permissions, 'READ')}">Can read</div>

<!-- Set conversion -->
<span th:text="${#sets.toList(stringSet)}">List</span>
<span th:text="${#sets.toSet(list)}">Set</span>
```

### #arrays

```html
<!-- Array operations -->
<span th:text="${#arrays.length(array)}">0</span>
<span th:text="${#arrays.contains(array, 'item')}">Contains</span>
<span th:text="${#arrays.toString(array)}">String</span>
```

### #bools

```html
<!-- Boolean operations -->
<div th:if="${#bools.isTrue(condition)}">True</div>
<div th:if="${#bools.isFalse(condition)}">False</div>
<span th:text="${#bools.xor(a, b)}">XOR</span>
<span th:text="${#bools.and(a, b, c)}">AND</span>
```

## Expression Operators

### Arithmetic Operators

```html
<!-- Basic arithmetic -->
<span th:text="${price * quantity}">Total</span>
<span th:text="${total - discount}">Discounted</span>
<span th:text="${count / 2}">Half</span>
<span th:text="${count % 2 == 0}">Even</span>
```

### Comparison Operators

```html
<!-- Comparisons -->
<div th:if="${age >= 18}">Adult</div>
<div th:if="${score > 90}">Excellent</div>
<div th:if="${status == 'ACTIVE'}">Active</div>
<div th:if="${name != null}">Has name</div>
```

### Logical Operators

```html
<!-- Logical operations -->
<div th:if="${user and admin}">User and Admin</div>
<div th:if="${active or pending}">Active or Pending</div>
<div th:if="${not empty}">Not empty</div>
<div th:if="${!(error)}">No error</div>
```

### Conditional Operator

```html
<!-- Ternary operator -->
<span th:text="${user ? user.name : 'Guest'}">Name</span>
<span th:text="${score >= 60 ? 'Pass' : 'Fail'}">Result</span>
<span th:text="${count > 0 ? count : 'None'}">Count</span>
```

### Special Operators

```html
<!-- Elvis operator -->
<span th:text="${user.name ?: 'Anonymous'}">Name</span>
<span th:text="${config.timeout ?: 30000}">Timeout</span>

<!-- Safe navigation -->
<span th:text="${user?.name}">Name</span>
<span th:text="${user?.address?.city}">City</span>

<!-- No-operation -->
<span th:text="${_}">No change</span>
```

## Precedence Rules

1. Parentheses `()`
2. Navigation operators `.` `[]`
3. Unary operators `-` `!` `not`
4. Multiplication/division `*` `/` `%`
5. Addition/subtraction `+` `-`
6. Comparison operators `>` `<` `>=` `<=` `==` `!=`
7. Logical operators `and` `or`
8. Elvis operator `?:`
9. Assignment `=`

## Type Conversion

### Automatic Conversion

```html
<!-- String to number -->
<span th:text="${'123' + 1}">124</span>

<!-- Number to string -->
<span th:text="${123 + ' items'}">123 items</span>

<!-- Boolean conversion -->
<div th:if="${'true'}">True</div>
<div th:if="${'false'}">False</div>
```

### Manual Conversion

```html
<!-- Using conversion service -->
<span th:text="${#conversions.convert(object, 'java.lang.String')}"
  >String</span
>
<span th:text="${#conversions.convert(string, 'java.lang.Integer')}"
  >Integer</span
>
```
