3

I am using Liferay 6.2 and freemaker for web content templates. I need to get request object in template. When I try to use ${Request.getRequest()} it throws error Expression Request is undefined. Full code here

<#assign userService=utilLocator.findUtil('my-portlets', org.test.service.UserService')>
 <#if userService.isUser(Request.getRequest())>
     <h1>is User</h1>
 </#if>

My handling method for userService

public Boolean isUser(HttpServletRequest request) {
    //some logic
    return true;
}        

How I can get Request object in freemaket ftl template?

jahra
  • 1,173
  • 1
  • 16
  • 41
  • `$request` is an implicit object available in templates. – Parkash Kumar Jan 15 '16 at 10:14
  • 1
    However, if you want to get `httpServletRequest` object, you can get it by: `#set ($serviceContext = $portal.getClass().forName("com.liferay.portal.service.ServiceContextThreadLocal").getServiceContext()) #set ($httpServletRequest = $serviceContext.getRequest())` – Parkash Kumar Jan 15 '16 at 10:15
  • Thank you! But can you post your answer in more expanded way with a little example? @ParkashKumar – jahra Jan 15 '16 at 10:21
  • Did it worked in you case? – Parkash Kumar Jan 15 '16 at 10:24
  • No, it falls with `Unknown directive: #set` @ParkashKumar – jahra Jan 15 '16 at 10:29
  • Ohhh, you are using `ftl` language in templates, then you can use, `<#assign>` instead of `#set`. – Parkash Kumar Jan 15 '16 at 10:31
  • You can use it like: `<#assign $serviceContext = $portal.getClass().forName("com.liferay.portal.service.ServiceContextThreadLocal‌​").getServiceContext()> <#assign $httpServletRequest = $serviceContext.getRequest()>` – Parkash Kumar Jan 15 '16 at 10:33
  • Now `Expression $portal is undefined` :( @ParkashKumar – jahra Jan 15 '16 at 10:34
  • Check for your property `freemarker.engine.restricted.variables=` in `portal.properties` and ensure it doesn't add `$portal` to its list. – Parkash Kumar Jan 15 '16 at 10:37
  • 1
    Here is complete list of [***Implicit Objects & Variables***](https://www.liferay.com/community/wiki/-/wiki/Main/Access+Objects+from+Velocity) – Parkash Kumar Jan 15 '16 at 10:40
  • 1
    And here is the [***Answer***](https://www.liferay.com/community/forums/-/message_boards/message/34142230#_19_message_34310408), that matched your question. – Parkash Kumar Jan 15 '16 at 10:44

3 Answers3

1

FreeMarker variables are case sensitive and don't start with the $ sign (like in velocity).

Use the request variable.

rostanek
  • 623
  • 4
  • 11
1

You can just use the request variable like in

<#if request.getParameter("something")??>

I find this file always useful

https://github.com/agmip/liferay-portal-impl/blob/master/src/com/liferay/portal/freemarker/FreeMarkerVariablesImpl.java

Also, the use of $ is probably causing you some issues there, as it is not used as it is in velocity..

Victor
  • 3,520
  • 3
  • 38
  • 58
0

Simply use: themeDisplay.getRequest()

<#assign cookies = themeDisplay.getRequest().getCookies() />
<#list cookies as cookie>
  ${cookie.name} = ${cookie.value}<br>
</#list>