Skip to content

Instantly share code, notes, and snippets.

@banterCZ
Created April 9, 2013 11:27
Show Gist options
  • Save banterCZ/5344966 to your computer and use it in GitHub Desktop.
Save banterCZ/5344966 to your computer and use it in GitHub Desktop.
Using JSF, there is no nice way how to iterate over collection an include a template.
<ui:composition template="/WEB-INF/templates/layout.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:c="http://java.sun.com/jstl/core">
<ui:define name="content">
<!-- does NOT work -->
<!-- JSTL tags are taghandlers and they are executed during view build time, while JSF UI components are executed during view render time -->
<!-- http://stackoverflow.com/a/3343681/204950 -->
<c:forEach var="item" items="#{items}">
<h:panelGroup rendered="#{item.show}">
<ui:include src="${item.template}" />
</h:panelGroup>
</c:forEach>
<!-- does NOT work -->
<!-- <ui:include> runs during view build time (restore view phase) not during view render time (render response phase) -->
<!-- http://stackoverflow.com/a/13992285/204950 -->
<ui:repeat value="#{items}" var="item">
<h:panelGroup rendered="#{item.show}">
<ui:include src="${item.template}" />
</h:panelGroup>
</ui:repeat>
<!-- works, but duplicity hell -->
<h:panelGroup rendered="#{items[0].show}">
<ui:include src="#{items[0].template}" />
</h:panelGroup>
<h:panelGroup rendered="#{items[1].show}">
<ui:include src="#{items[1].template}" />
</h:panelGroup>
<h:panelGroup rendered="#{items[2].show}">
<ui:include src="#{items[2].template}" />
</h:panelGroup>
</ui:define>
</ui:composition>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment