user10777196
user10777196

Reputation:

Include meta to JSF

I have a JSF page which include <head>...</head> from another page. Basically it's looks like this:

<ui:include src="/path/head.xhtml"/>
<h:body>
...
</h:body>

I want to add unique <meta name="description" content="Unique content"> to each page which use <ui:include src="/path/head.xhtml"/>. I have 2 ideas:

  1. Can I put <meta name="description".../> to head-page and overwrite it on include-pages?
  2. Or maybe I can put somehow this <meta.../> from include pages to head?

Upvotes: 1

Views: 385

Answers (1)

BalusC
BalusC

Reputation: 1108567

Several ways:

  1. Use <ui:insert> without name and have body of <ui:include> override it:

    /WEB-INF/includes/head.xhtml

    <h:head>
        ...
        <ui:insert>
            <meta name="description" content="Default description" />
        </ui:insert>
        ...
    </h:head>
    

    /page.xhtml

    <ui:include src="/WEB-INF/includes/head.xhtml">
        <meta name="description" content="Overridden description" />
    </ui:include>
    
  2. Use <ui:insert> with name, and use <ui:decorate><ui:define> instead of <ui:include>:

    /WEB-INF/includes/head.xhtml

    <h:head>
        ...
        <ui:insert name="description">
            <meta name="description" content="Default description" />
        </ui:insert>
        ...
    </h:head>
    

    /page.xhtml

    <ui:decorate template="/WEB-INF/includes/head.xhtml">
        <ui:define name="description">
            <meta name="description" content="Overridden description" />
        </ui:define>
    </ui:decorate>
    
  3. Simply use <ui:param> with solely the meta content to keep code DRY when you already have a default description:

    /WEB-INF/includes/head.xhtml

    <h:head>
        ...
        <meta name="description" content="#{empty description ? 'Default description' : description}" />
        ...
    </h:head>
    

    /page.xhtml

    <ui:include src="/WEB-INF/includes/head.xhtml">
        <ui:param name="description" value="Overridden description" />
    </ui:include>
    

See also:

Upvotes: 3

Related Questions