从Velocity模板访问Liferay自定义字段

时间:2012-11-16 01:14:18

标签: liferay velocity liferay-6

我正在尝试从其中一个Liferay模板中发出自定义字段的值。

使用管理界面,我定义了一个名为“org-home-page”的新组织级自定义字段,默认值为“tom rules”。

我想在portal_normal.vm

中发出此值

我根据同事发送的一些帖子和样本拼凑了这些代码,以及我自己的大量实验:

$page.getGroup().getExpandoBridge().getAttribute("org-home-page")

不幸的是,Velocity无法解析表达式,并且保持不变。

以下表达式在portal_normal中进行评估,但显然这些语句都不能完成整个工作:

$page                               ## seems to represent the current page
$page.getGroup()                    ## seems to represent the current Org
$page.getGroup().getExpandoBridge() ## seems to give me an "Expando bridge" object

只有最后一步 - 我通过名称识别我想要检索其值的特定自定义字段 - 失败。

我不允许编写任何自定义Java来促进这一点,所以不要打扰启动Eclipse。 8)只有完全可以在Velocity模板中实现的解决方案才是可接受的。

感谢任何帮助。

2 个答案:

答案 0 :(得分:4)

我能够在Liferay Portal 6.1.0中使用以下方法获取组织的自定义字段的值。也许它太冗长了,但至少它是有效的。 :)

init_custom.vm

...
## Null variable
#set($null = $some-never-used-variable-name)
...
#set($organization = $null)
#if ($layout.getGroup().isOrganization())
    ## Get organization by id
    #set($organizationLocalService = $serviceLocator.findService("com.liferay.portal.service.OrganizationLocalService"))
    #set($organizationId = $layout.getGroup().getOrganizationId())
    #set($organization = $organizationLocalService.getOrganization($organizationId))
#end
...

portal_normal.vm

...
#if ($organization != $null)
    ## Use value of custom field of organization
    $organization.getExpandoBridge().getAttribute("org-home-page")
#end    
...

答案 1 :(得分:0)

在Liferay 7 +中为我工作:

创建自定义字段类型" site",将数据填充到网站设置中,并使用主题模板将此数据调用到liferay主题:

如果是VM文件:

#set ($site_custom_field = $layout.getGroup().getExpandoBridge().getAttribute("site_custom_field_key"))
<h1>$site_custom_field</h1>

如果是FTL文件:

<#assign site_custom_field = layout.getGroup().getExpandoBridge().getAttribute("site_custom_field_key")>
<h1>${site_custom_field}</h1>
相关问题