Facelets template within another template
Asked Answered
A

1

5

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.

Aribold answered 27/9, 2012 at 14:10 Comment(1)
Just do so? In the new template, just do <ui:composition template="base.xhtml"> and so on? What's the concrete problem while doing that?Grecian
G
10

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?

Grecian answered 27/9, 2012 at 14:17 Comment(2)
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?Aribold
You're welcome. There's too much ambiguity in the problem description. Feel free to ask a new question along with some concrete code.Grecian

© 2022 - 2024 — McMap. All rights reserved.