【问题标题】:How to get active theme name in velocity template in Liferay如何在 Liferay 的速度模板中获取活动主题名称
【发布时间】:2013-09-30 20:02:40
【问题描述】:

我们为桌面网页使用一个主题,为网站的响应式/移动版本使用另一个主题。但是对于移动设备,我们并不总是希望显示图像,如果我们可以从模板中限制它会很好,而不仅仅是用 CSS 隐藏它(因为它仍然加载图像,只是不显示它)。

为此,我需要获取 liferay 当前活动主题 ID 或名称,以便在模板中添加简单的 if 语句。

#if(${themename} == "desktopTheme")
<img src="foo.jpg"/>
#end

有人知道如何获得它吗?我搜索并检查了许多 liferay 的论坛,但没有找到任何东西。

谢谢!

【问题讨论】:

    标签: liferay liferay-velocity liferay-theme


    【解决方案1】:

    我的建议是不要对主题名称进行硬编码,而是使用现成的theme settings。只需将您的主题配置为“桌面”或“移动”样式并检查此属性。这样您就可以涵盖许多不同的主题,而无需对特定主题进行硬编码。

    此外,各种ThemeDisplay.getTheme*() 方法将帮助您在几乎任何地方获得所需的信息。

    这是您主题的一些未经测试的伪代码(我只是在这里输入,从未编译/部署,您可能想为 VM 内容添加空检查)

    <?xml version="1.0"?>
    <!DOCTYPE look-and-feel PUBLIC "-//Liferay//DTD Look and Feel 6.0.0//EN" "http://www.liferay.com/dtd/liferay-look-and-feel_6_0_0.dtd">
    <look-and-feel>
        <compatibility>
            <version>6.1.0+</version>
        </compatibility>
        <theme id="my-desktop-1" name="My First Desktop Theme">
            <settings>
                <setting key="mobile" value="false" />
            </settings>
        </theme>
    </look-and-feel>
    
    
    <?xml version="1.0"?>
    <!DOCTYPE look-and-feel PUBLIC "-//Liferay//DTD Look and Feel 6.0.0//EN" "http://www.liferay.com/dtd/liferay-look-and-feel_6_0_0.dtd">
    <look-and-feel>
        <compatibility>
            <version>6.1.0+</version>
        </compatibility>
        <theme id="my-mobile-1" name="My First Mobile Theme">
            <settings>
                <setting key="mobile" value="true" />
            </settings>
        </theme>
    </look-and-feel>
    

    然后,在 VM 或 JSP 或其他代码中:

    #if( ! $themeDisplay.getSetting("mobile"))
         ## more resources, desktop only, here.
    #end
    
    #if( $themeDisplay.getSetting("mobile")
         ## mobile only stuff here.
    #end
    

    最后,您可能还想考虑使用Device Detection API 以更精细的粒度确定当前设备的实际功能

    【讨论】:

    • 嘿,谢谢!不知何故,它不起作用。我应该启用任何东西来从模板访问 $themeDisplay 吗?
    • 模板是指网页内容模板,而不是像portal_normal.vm这样的主题模板
    【解决方案2】:

    建议你使用themeId whis 也是独一无二的。它的内容如下: xxx_WAR_xxx主题

    #if($themeDisplay.getThemeId().equalsIgnoreCase("<dektop theme id>"))
    ### your logic here
    #end
    

    HTH

    【讨论】:

    • 如果我只添加到模板 $themeDisplay.getThemeId() 它不会呈现它?你知道我在哪里可以找到身份证吗?
    • 在我的答案中查看伪代码(实际上是 xml)以找到主题的 id
    猜你喜欢
    • 2014-05-27
    • 2013-01-17
    • 1970-01-01
    • 2014-04-12
    • 1970-01-01
    • 2015-07-24
    • 2013-10-07
    • 1970-01-01
    • 2013-08-13
    相关资源
    最近更新 更多