Iteration variable lost when using th:substituteby?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Iteration variable lost when using th:substituteby?

Emanuel
I'm trying to create some HTML that will look like this:

<section>
  <article></article>
  <article></article>
  ...
</section>

The code I tried to use to generate that looks something like this:

<section>
  <div th:each="post,p: ${blog.posts}" th:include="templates/blog/Post :: post"></div>
</section>

Where templates/blog/Post is an HTML file with the fragment for what a blog post should be.  Within Post.html it makes references to the post attribute with things like ${post.id}, ${post.content}, and so on.

What that did was create HTML like this:

<section>
  <div>
    <article></article>
  </div>
  <div>
    <article></article>
  </div>
  ...
</section>

So I thought that I should use the th:substituteby instead of th:include, since that will actually switch the <div> out for the <article>, but doing so creates a NullPointerException, saying that ${post} is null when it gets to Post.html:

org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating OGNL expression: "post.id" (templates/blog/Post:15)
...
Caused by: ognl.OgnlException: source is null for getProperty(null, "id")

Is this actually by design since th:substituteby replaces the <div> tag, thus losing the th:each declaration that I made in it?
Reply | Threaded
Open this post in threaded view
|

Re: Iteration variable lost when using th:substituteby?

danielfernandez
Administrator
Exactly. th:substituteby gets executed before your th:each, so you never really get a th:each executed there, I'm afraid...

Regards,
Daniel