Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facelets template within another template

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.

like image 976
nivis Avatar asked Jan 15 '23 11:01

nivis


1 Answers

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?

like image 192
BalusC Avatar answered Jan 21 '23 16:01

BalusC