Facelets Template Includes and Params

| Comments

Facelets is a great template framework that sits on top of JSF. They have some fun template includes and parameters that you can pass into the included templates. Here is an example with one gotcha pertaining to the parameter name.

If I want to do an include and pass in some sort of parameter value, I could do this:

1
2
3
<ui:include src="inclusion.xhtml">
  <ui:param name="ilikebeans" value="true" />
</ui:include>

Thus, in the inclusion.xhtml file, I could reference #{ilikebeans}, and the value would be “true”.

Parameters can also be included on child templates and then read on parent templates. Here’s an example:

Parent:

1
2
3
4
5
6
<ui:composition>
  <!-- ... -->
  <ul>
    <li class="#{tab eq 'info' ? 'selected' : ''}">Info tab</li>
  </ul>
</ui:composition>

Child:

1
2
3
4
<ui:composition template="parent.xhtml">
  <!-- ... -->
  <ui:param name="tab" value="info" />
</ui:composition>

Thus, the child template can have certain values that affect the parent. As seen in the example, this could be useful for showing certain tabs in a list as selected when that particular page (the child) is shown.

One note on my findings for parameter names… they cannot have hyphens (-) in the name! Otherwise, the value does not come thru properly. Beats me!

Have fun with Facelets!

Comments