Thymeleaf: create a list of elements statically

The following code can be used to statically create a list of elements (numbers, strings, etc) inside a Thymeleaf view.

A list of integers:

intList = ${ {1, 2, 3, 4} }

A list of strings:

strList = ${ {'netgloo', 'web', 'design'} }

Usage example

<div th:with="strList = ${ {'netgloo', 'web', 'design'} }">

  <!-- Some code using ${strList} -->
  <ul>
    <li th:each="s : ${strList}" th:text="|${s}|"></li>
  </ul>
 
</div>

References

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/expressions.html#expressions-language-ref

  • Kumar

    How to add another element when list is already declared

    • Hello Kumar, try to take a look to this answer on Stackoverflow if can help you: https://stackoverflow.com/a/26109614/3763649

      In general, as said in the above answer also, it would be better to modify the list in your controller, before it is passed to Thymeleaf. And use Thymeleaf just to render the list.

Categories

Category BootstrapCategory CoffeescriptCategory DrupalCategory GravCategory HTMLCategory JavascriptCategory JoomlaCategory jQueryCategory LaravelCategory MagentoCategory PHPCategory SharePointCategory SpringCategory ThymeleafCategory WordPressCategory Workflow

Comments

Developed and designed by Netgloo
© 2017 Netgloo