<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/thymeleaf-iteration-and-fragments-oiyon0aq2" -->

---
title: Thymeleaf iteration and fragments | daily.dev
description: When combining Thymeleaf's `th:each` iteration with `th:insert` or `th:replace` fragment directives, a common pitfall arises: Thymeleaf processes...
canonical: https://daily.dev/posts/thymeleaf-iteration-and-fragments-oiyon0aq2
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Thymeleaf iteration and fragments | daily.dev
og:description: When combining Thymeleaf's `th:each` iteration with `th:insert` or `th:replace` fragment directives, a common pitfall arises: Thymeleaf processes...
og:url: https://daily.dev/posts/thymeleaf-iteration-and-fragments-oiyon0aq2
og:image: https://api.daily.dev/og/posts/oiYON0Aq2.png
og:image:alt: Thymeleaf iteration and fragments
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Thymeleaf iteration and fragments

**[Wim Deblauwe](https://daily.dev/sources/wim-deblauwe)** · 1 min read · 0 upvotes · 0 comments

## Summary

When combining Thymeleaf's `th:each` iteration with `th:insert` or `th:replace` fragment directives, a common pitfall arises: Thymeleaf processes `th:insert`/`th:replace` before `th:each`. This means that using `th:replace` on an element that also has `th:each` causes the iteration attribute to be lost when the element is replaced by the fragment, resulting in the loop variable being undefined and a template processing error.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.wimdeblauwe.com/blog/2021/09/14/thymeleaf-iteration-and-fragments/>

## Similar posts on daily.dev

- [How to Put a Text Without Removing HTML Structures in Thymeleaf](https://daily.dev/posts/how-to-put-a-text-without-removing-html-structures-in-thymeleaf-qbyxk5mji) · Baeldung · 0 upvotes · 0 comments
- [Writing a Thymeleaf component library - part 2](https://daily.dev/posts/writing-a-thymeleaf-component-library---part-2-ibhbku0a4) · Wim Deblauwe · 1 upvotes · 0 comments
- [foojay – a place for friends of OpenJDK](https://daily.dev/posts/foojay-a-place-for-friends-of-openjdk-rdat3ew2h) · Foojay.io · 1 upvotes · 0 comments
- [Recursion is DEAD and HTML killed it](https://daily.dev/posts/recursion-is-dead-and-html-killed-it-v4n6nrobh) · Dave Rupert · 28 upvotes · 0 comments
- [Calling an Object’s Method From Thymeleaf](https://daily.dev/posts/calling-an-object-s-method-from-thymeleaf-vksmbvbzd) · Baeldung · 3 upvotes · 0 comments

---

Tags: [#java](https://daily.dev/tags/java)

[View this post on daily.dev](https://daily.dev/posts/thymeleaf-iteration-and-fragments-oiyon0aq2)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Thymeleaf iteration and fragments","url":"https://daily.dev/posts/thymeleaf-iteration-and-fragments-oiyon0aq2","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/thymeleaf-iteration-and-fragments-oiyon0aq2"},"datePublished":"2026-03-16T08:15:01.329Z","dateModified":"2026-03-16T08:24:31.368Z","description":"When combining Thymeleaf's `th:each` iteration with `th:insert` or `th:replace` fragment directives, a common pitfall arises: Thymeleaf processes...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/1eb14f32268fcda2ecbc4738a73c76d0?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/1eb14f32268fcda2ecbc4738a73c76d0?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Wim Deblauwe","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Wim Deblauwe","logo":"https://media.daily.dev/image/upload/s--g1qrglrf--/c_limit,w_256/f_auto,q_auto/v1773648779/logos/wim-deblauwe?_a=BAMAMiiu0","url":"https://daily.dev/sources/wim-deblauwe"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/thymeleaf-iteration-and-fragments-oiyon0aq2","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"java","timeRequired":"PT1M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Wim Deblauwe","item":"https://daily.dev/sources/wim-deblauwe"},{"@type":"ListItem","position":3,"name":"Thymeleaf iteration and fragments"}]}
```

