Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Archive and tags pages not working as designed when using the Thymeleaf sample project #534

Closed
vindevoy opened this issue Aug 25, 2018 · 4 comments

Comments

@vindevoy
Copy link

vindevoy commented Aug 25, 2018

Hi,

I have started to develop my blog with JBake + Thymeleaf. When adding features, I discovered that you cannot update the value of a variable in thymeleaf. But, that is used in the templates/archive.thyme and templates/tags.thyme.

<div th:each="post : ${posts}" th:with='last_month=null' th:remove='tag'>
  <h3 th:if='${last_month!=#dates.format(post.date,"MMMM yyyy")}' th:text='${#dates.format(post.date,"MMMM yyyy")}'>June 2014</h3>
		
  <h4><span th:text='${#dates.format(post.date,"dd")}' th:remove='tag'/> - <a th:href="${content.rootpath+post.uri}" th:text='${post.title}' href="foo.html">Post title</a></h4> 
 <span th:remove='all' th:with='last_month = ${#dates.format(post.date,"MMMM yyyy")}'/>
</div>

You set the variable and then update it, but that does not work. As soon as you leave scope, your variable is reset. So, after the span (so basically immediately) your last_month is null again. When you run the site, you will see that there is no grouping as with other techniques.

I really wanted grouping, so I updated the code to have this working. I hereby post my solution. The solution is similar for both pages, just updated published_posts with tag_posts, below is the code for archive.


<th:block th:with="total_posts = *{#lists.size(published_posts)}">
  <th:block th:if="*{total_posts != 0}" th:with="first_post = *{posts[0]}">
    <h4 th:text="*{#dates.format(first_post.date,'MMMM yyyy')}"></h4>
    <span class="font-weight-bold" th:text="*{#dates.format(first_post.date,'dd')}"/> -
    <a th:href="*{content.rootpath + first_post.uri}" th:text="*{first_post.title}" href=""></a>
    <br/>

    <th:block th:if="*{total_posts &gt; 1}" th:each="i : *{#numbers.sequence( 1, total_posts - 1)}"> 
      <th:block th:with="previous_post = *{posts[i - 1]}, current_post = *{posts[i]}">
        <th:block th:if="*{#dates.format(previous_post.date,'MMMM yyyy')} != *{#dates.format(current_post.date,'MMMM yyyy')}">
          <p></p>
	  <h4 th:text="*{#dates.format(current_post.date,'MMMM yyyy')}"></h4>
        </th:block>

        <span class="font-weight-bold" th:text="*{#dates.format(current_post.date,'dd')}"/> -
        <a th:href="*{content.rootpath + current_post.uri}" th:text="*{current_post.title}" href=""></a> 
        <br/>
      </th:block>
    </th:block>
  </th:block>
</th:block>


My blog is still in development phase, but you can see the first results in http://dv.blog.yves.vindevogel.net. The final URL will be without the dv.

I have my own GitLab running, but I am pushing the changes on protected branches (develop and master) also to GitHub: https://github.com/vindevoy/blog-yves-vindevogel-net.git

Kr,
Yves Vindevogel (vindevoy)

@jonbullock
Copy link
Member

Hi Yves,

Yeah this was raised on the Thymeleaf example project repo: jbake-org/jbake-example-project-thymeleaf#5

If you have time would you mind raising a PR on the Thymeleaf example project repo with your fix applied to archive and tags?

Thanks

@vindevoy
Copy link
Author

vindevoy commented Oct 27, 2018 via email

@jonbullock
Copy link
Member

Thanks @vindevoy have a look at #370 for the long term goal for 3rd party templates.

@jonbullock jonbullock added this to the v2.6.4 milestone Nov 16, 2018
@jonbullock
Copy link
Member

Merged in fix for default Thymeleaf example project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants