Generate list of objects in markup

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

Generate list of objects in markup

jg
I got this piece of template:

<ul class="navigation" th:each="menu : ${menus}">
  <li>
    <a href="${menu.URL}">
      <span class="navigationitem">${menu.title}</span>
   </a>
  </li>
</ul>

Is there a way to create a list of valid objects in markup?
<foo:menu th:attr="menus=?" />

The final result will of course not use this, but it would be easy for quick testing purposes.


Thanks!
Reply | Threaded
Open this post in threaded view
|

Re: Generate list of objects in markup

danielfernandez
Administrator
If I understand you correctly, you can define your piece of markup as a fragment somewhere in your tempaltes (for example, in a "fragments.html" file):

    <div th:fragment="menufrag">
        <ul class="navigation" th:each="menu : ${menus}">
          <li>
            <a href="${menu.URL}">
              <span class="navigationitem">${menu.title}</span>
           </a>
          </li>
        </ul>
    </div>

...and then use it from anywhere with a combination of "th:include" and "th:with":

    ...
    <div th:include="fragments :: menufrag" th:with="menus=${...}"></div>
    ...

Regards,
Daniel.