测试是否在模板客户端中定义了ui:insert [英] Test if ui:insert has been defined in the template client

查看:154
本文介绍了测试是否在模板客户端中定义了ui:insert的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能知道ui:composition中是否定义了ui:insert. 我知道我可以使用单独的ui:param来做到这一点,但只是想做到这一点而不是为了使其简单且不易出错.

I am wondering if it is possible to know if ui:insert was defined in the ui:composition. I know that I can do it using separate ui:param, but just wanted to do it without in order to keep it simple and less error prone.

示例:

模板

...
<ui:insert name="sidebar" />

<!-- Conditionnaly set the class according if sidebar is present or not -->
<div class="#{sidebar is defined ? 'with-sidebar' : 'without-sidebar'}">
    <ui:insert name="page-content" />
</div>
...

第1页

...
<ui:define name="sidebar">
    sidebar content
</ui:define>

<ui:define name="page-content">
    page content
</ui:define>
...

第2页

...
<ui:define name="page-content">
    page content
</ui:define>
...

推荐答案

ui:param对我来说是最好的选择.只是以正确的方式使用它.作为一个简单的示例,我在此处定义一个参数以指定是否有侧边栏.请记住,您可以在模板中定义默认的插入定义,因此只需在内部声明即可:

ui:param is for me the best way to go. It's just a matter of using it the right way. As a simple example, I define a param here to specify wether there's a sidebar or not. Keep in mind you can define a default insertion definition in the template, so just declare it inside:

template.xhtml

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html">

    <ui:insert name="sidebar">
        <!-- By default, there's no sidebar, so the param will be present.
            When you replace this section for a sidebar in the client template,
            the param will  be removed from the view -->
        <ui:param name="noSideBar" value="true" />
    </ui:insert>

    <div class="#{noSideBar ? 'style1' : 'style2'}">
        <ui:insert name="content" />
    </div>

</ui:composition>

然后在这里有两个视图,一个使用侧边栏,另一个不带侧边栏.您可以对其进行测试,并查看浏览器中样式的变化.您会注意到第二个中的#{noSideBar}没有值,任何EL条件语句中的值都将为false.

Then couple of views here, one using the sidebar and the other with no sidebar. You can test it and see how the style changes in the browser. You'll notice there's no value for #{noSideBar} in the second one, which will evaluate to false in any EL conditional statement.

page1.xhtml

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets" template="/template.xhtml">
    <ui:define name="content">
        No sidebar defined? #{noSideBar}
    </ui:define>
</ui:composition>

page2.xhtml

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets" template="/template.xhtml">
    <ui:define name="sidebar" />
    <ui:define name="content">
        No sidebar defined? #{noSideBar}
    </ui:define>
</ui:composition>

这样,您只需要担心在客户端视图中是否包含侧边栏.

This way you only need to worry about including the sidebar or not in the client view.

这篇关于测试是否在模板客户端中定义了ui:insert的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆