4

I would like to use a Facelets template within another template. Currently I have a "base" template that so far has been enough for all pages I've done. It has a top and a content area.

The top has logo, menu, login/logout functionality, while the content area shows, well, the content.

Now I need to do another page (to hold user profile information) where I'd like to have a menu to the left and show result to the right. This page shall be inserted in the base template content area.

Is it possible to create a new template which defines those two areas (profile_left and profile_content) and somehow still use the base template?

I see no reason why I couldn't just copy the code in the base template and add the new "defines" that I want (profile_left and profile_content), but I still wonder if it's possible to keep using the original base template.

BalusC
  • 1,040,783
  • 362
  • 3,548
  • 3,513
nivis
  • 893
  • 3
  • 16
  • 33
  • Just do so? In the new template, just do `` and so on? What's the concrete problem while doing that? – BalusC Sep 27 '12 at 14:13

1 Answers1

9

You can extend from templates as deep as you want. It's not true that you can extend from only one template or something as you seem to think.

For example:

/WEB-INF/templates/base.xhtml

<!DOCTYPE html>
<html lang="en"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
>
    <h:head>
        <title><ui:insert name="title">Default title</ui:insert></title>
    </h:head>
    <h:body>
        <div id="header">Header</div>
        <div id="menu">Menu</div>
        <div id="content"><ui:insert name="content">Default content</ui:insert></div>
        <div id="footer">Footer</div>
    </h:body>
</html>

/WEB-INF/templates/profile.xhtml

<ui:composition template="/WEB-INF/templates/base.xhtml"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
>
    <ui:define name="content">
        <div id="profile_left"><ui:insert name="profile_left" /></div>
        <div id="profile_right"><ui:insert name="profile_right" /></div>
    </ui:define>
</ui:composition>

/user.xhtml

<ui:composition template="/WEB-INF/templates/profile.xhtml"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
>
    <ui:define name="title">User profile</ui:define>
    <ui:define name="profile_left">
        Profile left.
    </ui:define>
    <ui:define name="profile_right">
        Profile right.
    </ui:define>
</ui:composition>

See also:

How to include another XHTML in XHTML using JSF 2.0 Facelets?

Community
  • 1
  • 1
BalusC
  • 1,040,783
  • 362
  • 3,548
  • 3,513
  • Thank you BalusC. I tested changing the code to mimic your example and found that it worked, although I ended up with a small problem. Currently, in base.xhtml, I'm having a ui:insert in the top content. The reason being that I read somewhere that it was best practice to do like that. Therefore I don't get the menu just the default text "content top" that I entered in the ui:insert. Is it your view that I should do the include of the menu directly in the base template instead? – nivis Sep 27 '12 at 14:48
  • You're welcome. There's too much ambiguity in the problem description. Feel free to ask a new question along with some concrete code. – BalusC Sep 27 '12 at 15:17