There are number of predefined variables that can be accessed from with in the liferay theme. For elaborated list of those, below is the url
http://www.liferay.com/community/wiki/-/wiki/Main/Access+Objects+from+Velocity?_36_pageResourcePrimKey=2905717
Apart from those variables, if at all we end up needing a custom variable, which define a custom functionality/value as per the specific business need, we can define them in two places.
1. init_custom.vm.
init.vm file, which is present in the liferay theme, contains all the liferay defined velocity variables. As a best practice, all the custom variables should be defined in the init_custom.vm file.
This file allows you to override and define new Velocity variables.
#set($background_url = $theme_display.getThemeSetting('background-url'))
In the above example we are reading the background-url value provided by the user, while configuring the themes.
2. Setting the variables in request scope under attribute WebKeys.VM_VARIABLES
Create a hook, either Service Pre action or Login Post Action hook and inside your extended pre or post Action class, create a Map object with the key and value of the vm variables you want to insert, and then set that into the attribute under the key WebKeys.VM_VARIABLES. The variables added to the map are directly accessible in the Theme. This method provide more flexibility than the earlier one as there are no restrictions for defining the variables in this method.
Here is the code snippet for the same
Map< String, Object > vmVariables = ( Map< String, Object > ) request.getAttribute( WebKeys.VM_VARIABLES );
if ( vmVariables == null ) { vmVariables = new HashMap< String, Object >();
}
vmVariables.put("someVariable", "someValue");
Now the variable added to the Map is directly accessible from with in the velocity template file in the theme.
For example it can be directly accessible from portal_normal.vm as below
http://www.liferay.com/community/wiki/-/wiki/Main/Access+Objects+from+Velocity?_36_pageResourcePrimKey=2905717
Apart from those variables, if at all we end up needing a custom variable, which define a custom functionality/value as per the specific business need, we can define them in two places.
1. init_custom.vm.
init.vm file, which is present in the liferay theme, contains all the liferay defined velocity variables. As a best practice, all the custom variables should be defined in the init_custom.vm file.
This file allows you to override and define new Velocity variables.
#set($background_url = $theme_display.getThemeSetting('background-url'))
In the above example we are reading the background-url value provided by the user, while configuring the themes.
2. Setting the variables in request scope under attribute WebKeys.VM_VARIABLES
Create a hook, either Service Pre action or Login Post Action hook and inside your extended pre or post Action class, create a Map object with the key and value of the vm variables you want to insert, and then set that into the attribute under the key WebKeys.VM_VARIABLES. The variables added to the map are directly accessible in the Theme. This method provide more flexibility than the earlier one as there are no restrictions for defining the variables in this method.
Here is the code snippet for the same
Map< String, Object > vmVariables = ( Map< String, Object > ) request.getAttribute( WebKeys.VM_VARIABLES );
if ( vmVariables == null ) { vmVariables = new HashMap< String, Object >();
}
vmVariables.put("someVariable", "someValue");
request.setAttribute( WebKeys.VM_VARIABLES, vmVariables );
Now the variable added to the Map is directly accessible from with in the velocity template file in the theme.
For example it can be directly accessible from portal_normal.vm as below
<a id="siteLogo" class="logo $logo_css_class" href="$someVariable" title="#language("go-to") $site_name"/>