Pages

Tuesday, 22 July 2014

Access Objects from Velocity

Objects Available in Themes, Layouts, and CMS #

In the theme, layout templates and CMS Templates we load all the tools found in the com.liferay.portal.velocity.VelocityVariables insertHelperUtilities() method:
  • arrayUtil
  • browserSniffer
  • dateFormats
  • dateTool
  • dateUtil
  • escapeTool
  • expandoColumnLocalService
  • expandoRowLocalService
  • expandoTableLocalService
  • expandoValueLocalService
  • getterUtil
  • htmlUtil
  • httpUtil
  • imageToken
  • iteratorTool
  • journalContentUtil
  • languageUtil
  • unicodeLanguageUtil
  • listTool
  • localeUtil
  • mathTool
  • numberTool
  • paramUtil
  • portalUtil
  • portal
  • prefsPropsUtil
  • propsUtil
  • portletURLFactory
  • velocityPortletPreferences
  • randomizer
  • saxReaderUtil
  • serviceLocator
  • sessionClicks
  • sortTool
  • staticFieldGetter
  • stringUtil
  • timeZoneUtil
  • utilLocator
  • unicodeFormatter
  • validator
  • accountPermission
  • commonPermission
  • groupPermission
  • layoutPermission
  • organizationPermission
  • passwordPolicyPermission
  • portalPermission
  • portletPermission
  • rolePermission
  • userGroupPermission
  • userPermission
  • locationPermission

Objects Which Can Be Restricted From Access by CMS #

We call these restricted variables. They are restricted by adding them to the following portal property:
    #
    # Input a comma delimited list of variables which are restricted from the
    # context in Velocity based Journal templates.
    #
    journal.template.velocity.restricted.variables=serviceLocator
Following are the list of variables that can be restricted:
  • portalUtil
  • portal
  • prefsPropsUtil
  • propsUtil
  • velocityPortletPreferences
  • serviceLocator
  • sessionClicks
  • utilLocator
Now, on the issue of what other variables are accessible where.

Run Time Variables #

In each of the three contexts there are some "run-time" variables which are available only in those contexts.

Layout Templates #

The only additional runtime variable for layouts is:
  • processor

Themes #

  • request
  • portletConfig
  • renderRequest
  • renderResponse
  • xmlRequest
  • themeDisplay
  • company
  • user
  • realUser
  • layout
  • layouts
  • plid
  • layoutTypePortlet
  • scopeGroupId
  • permissionChecker
  • locale
  • timeZone
  • theme
  • colorScheme
  • portletDisplay
  • navItems
  • fullCssPath
  • fullTemplatesPath
  • init
  • portletGroupId
  • processor
  • taglibLiferay
  • theme
These are all objects are you might expect them to be... real
HttpServletRequest
, real User, etc...

CMS Templates #

The processing of a template is handled in such a way that it is not dependent on access to a real HttpRequest of any kind. Why? Because it's done WITHIN the service call. We do not allow our service layer objects to have an HttpRequest or HttpResponse of any kind as parameters. Thus, we can't add any of those NON-serializable objects like
HttpServletRequest
,
ThemeDisplay
,
PortletDisplay
,
PortletPreferences
, etc... As such we have the following variables:
  • xmlRequest
  • request
  • company
  • companyId
  • groupId
  • journalTemplatesPath
  • viewMode
  • locale
  • permissionChecker
  • randomNamespace

Variables defined in init.vm #

From Alimozzaman blog:
These are the default Liferay 6 theme variables defined in init.vm.
I was searching but did not get any link ,so I hope this will help lot of
people also around  the globe. If anything goes  wrong, pls make a comment
and also try to add something that will help people around us.

## ———- Common variables ———- ##

$theme_display
$portlet_display
$theme_timestamp
$theme_settings
$css_class
$layout
$page_group
$liferay_toggle_controls
$liferay_dockbar_pinned
$css_folder
$images_folder
$javascript_folder
$templates_folder
$full_css_path
$full_templates_path
$css_main_file
$js_main_file
$company_id
$company_name
$company_logo
$company_logo_height
$company_logo_width
$company_url

$user_id
$is_default_user
$user_first_name
$user_middle_name
$user_last_name
$user_name
$is_male
$is_female
$user_birthday
$user_email_address
$language_id
$w3c_language_id
$time_zone
$user_greeting
$user_comments
$user_login_ip
$user_last_login_ip
$is_signed_in
$group_id

## ———- URLs ———- ##
$show_add_content
$add_content_text
$add_content_url
$layout_text
$layout_url
$show_control_panel
$control_panel_text
$control_panel_url
$show_home
$home_text
$home_url
$show_my_account
$my_account_text
$my_account_url
$show_page_settings
$page_settings_text
$page_settings_url
$show_sign_in
$sign_in_text
$sign_in_url
$show_sign_out
$sign_out_text
$sign_out_url
$show_toggle_controls
$toggle_controls_text
$toggle_controls_url
$update_available_url

## ———- Page ———- ##

$the_title
$selectable
$is_maximized
$is_freeform

$page_javascript_1
$page_javascript_2
$page_javascript_3
$layout
$page = $layout
$is_first_child
$is_first_parent
$the_title
$is_portlet_page

$all_portlets
$column_1_portlets
$column_2_portlets
$column_3_portlets
$column_4_portlets
$column_5_portlets
$maximized_portlet_id
$typeSettingsProperties
$page_javascript_1
$page_javascript_2
$page_javascript_3
$community_name
$css_class
$my_places_portlet_url
$community_default_public_url
$community_default_private_url
$community_default_url
$the_title
$layouts
$pages

## ———- Navigation ———- ##
$nav_items
$has_navigation

## ———- Staging ———- ##
$show_staging
$staging_text
## ———- My places ———- ##
$show_my_places
$my_places_text

## ———- Includes ———- ##
$dir_include
$bottom_include
$bottom_ext_include
$content_include
$top_head_include
$top_messages_include

## ———- Date ———- ##
$date
$current_time
$the_year
 
 Sample Example 

Write the below code in init_custom.vm


#set ($userLocalService= $serviceLocator.findService("com.liferay.portal.service.UserLocalService"))
#set ($user = $userLocalService.getUserById($request.get("theme-display").get("user-id")))
#set ($emailAddress = $user.getEmailAddress())

 

To get the view write the below code in portal_normal.vm

 <h1>$emailAddress</h1>

I Hope this will help for you in feature.