Fórum

Current URL in velocity variable

Ishan Sahore, modificado 11 Anos atrás.

Current URL in velocity variable

Junior Member Postagens: 56 Data de Entrada: 26/04/12 Postagens Recentes
Hi all,

I want to use the URL of current page in a velocity template variable.
Can anyone tell me the syntax as to which method to call to retrieve the URL?
thumbnail
Jignesh Vachhani, modificado 11 Anos atrás.

RE: Current URL in velocity variable

Liferay Master Postagens: 803 Data de Entrada: 10/03/08 Postagens Recentes
You can use $theme_display.getURLCurrent() in your theme velocity template
Hope this will work !!!
Ishan Sahore, modificado 11 Anos atrás.

RE: Current URL in velocity variable

Junior Member Postagens: 56 Data de Entrada: 26/04/12 Postagens Recentes
Jignesh Vachhani:
You can use $theme_display.getURLCurrent() in your theme velocity template
Hope this will work !!!



Thanks Jignesh,

It shows the relative URL. Isn't there a way to get the absolute URL?

Thanks,
Ishan
thumbnail
Paul ., modificado 11 Anos atrás.

RE: Current URL in velocity variable

Liferay Master Postagens: 522 Data de Entrada: 29/08/11 Postagens Recentes
You can combine it with
 $theme_display.getPortalURL()
Ishan Sahore, modificado 11 Anos atrás.

RE: Current URL in velocity variable (Resposta)

Junior Member Postagens: 56 Data de Entrada: 26/04/12 Postagens Recentes
Thanks Jignesh and Paul,

So the absolute URL can be retrieved by the following:

$theme_display.getPortalURL().$theme_display.getURLCurrent()



Thanks,
Ishan
thumbnail
Jacques Traore, modificado 10 Anos atrás.

RE: Current URL in velocity variable

Junior Member Postagens: 49 Data de Entrada: 21/01/13 Postagens Recentes
Hi all,
Why can't I access $theme_display?
Writting
$theme_display.getPortalURL()
in the "Launch editor" just prints "$theme_display.getPortalURL()" and not the content of the url.

I am using LR 6.1.1 with a customs structure and template.

Thanks
thumbnail
James Falkner, modificado 10 Anos atrás.

RE: Current URL in velocity variable

Liferay Legend Postagens: 1399 Data de Entrada: 17/09/10 Postagens Recentes
Jacques Traore:
Hi all,
Why can't I access $theme_display?
Writting
$theme_display.getPortalURL()
in the "Launch editor" just prints "$theme_display.getPortalURL()" and not the content of the url.

I am using LR 6.1.1 with a customs structure and template.

Thanks


There is a different set of variables available in Velocity, depending on whether you are writing a Theme template, or a Web Content template. This is detailed here.

For Web Content, there is no $theme_display. Instead, you get a $request Map that contains many of the items normally found in a ThemeDisplay object.

I often find myself calling services in Liferay that expect something that looks like a Theme Display, so I end creating a fake one, and populating it with stuff from the $request map (in particular, notice the $portalURL that I construct):



#set ($themeDisplay = $portal.getClass().forName("com.liferay.portal.theme.ThemeDisplay").newInstance())
#set ($portalURL = $httpUtil.getProtocol($request.attributes.CURRENT_URL) + "://" + $getterUtil.getString($request.theme-display.portal-url))

#set ($V = $themeDisplay.setPathImage($getterUtil.getString($request.theme-display.path-image)))
#set ($V = $themeDisplay.setPathMain($getterUtil.getString($request.theme-display.path-main)))
#set ($V = $themeDisplay.setPermissionChecker($permissionThreadLocal.getPermissionChecker()))
#set ($V = $themeDisplay.setPortalURL($portalURL))
#set ($V = $themeDisplay.setScopeGroupId($scopeGroupId))
#set ($V = $themeDisplay.setTimeZone($request.theme-display.time-zone))

## stuff...

#set ($userPic = $user.getPortraitURL($themeDisplay))

thumbnail
Jacques Traore, modificado 10 Anos atrás.

RE: Current URL in velocity variable

Junior Member Postagens: 49 Data de Entrada: 21/01/13 Postagens Recentes
Thank you James for replying,
Searching deeply, I've found that I have some troubles with the $request variable. I'm not sure to well understand how it works.
This is a use case:
1- I created an Asset Publisher to display custom web contents with custom structure and template.
2- In the template editor I wrote:

## Some set ...
#set ($redirect = $request.get("parameters").get("redirect"))
## Some set ...

#set ($axesMap = {"axis1" : "Axis 1" , "axis2" : "Axis 2" , "axis3" : "Axis 3" , })

...
Request = $request
  #if($redirect)
    <img class="ProfileImage" src="${Picture.getData()}" border="0" align="right" alt="">
    <p class="SubTitle">$Axes.getName()</p>    
    #foreach( $axis in $Axes.getOptions())
      <a class="Axis" href="/web/my-company/$axis">
        $axesMap.get($axis)
      </a>
      <br>
    #end

 ## Print some stuffs ....

  #else
    #foreach( $axis in $Axes.getOptions())
      <a class="Axis" href="/web/my-company/$axis">
        $axesMap.get($axis)
      </a>
      <br>
    #end
  #end

The first time the Asset Publisher is displayed, the $request is null (it shows {}) and after clicking on the web content title, the $request as a value.
At this point, it doesn't "very" matter. But when I edit a content and click on "Preview" button, the $request is {}.
However, I need its value, particularly the "cmd" parameter in preview and view (from history) modes to show the full content.
Therefore it only shows the abstract value of the content.
Same case when I try to visualize a published content from control panel (using Kaleo workflow plugin) before approve/reject it.

Otherwise, if you know another way to achieve this, may be creating many templates and doing association between view mode and template or whatever please let me know.

Thanks again
Henry K, modificado 10 Anos atrás.

RE: Current URL in velocity variable

Junior Member Postagens: 40 Data de Entrada: 27/01/10 Postagens Recentes
You can get the actual request and themeDisplay object in web content via:

#set ($serviceContext = $portal.getClass().forName("com.liferay.portal.service.ServiceContextThreadLocal").getServiceContext())
#set ($httpServletRequest = $serviceContext.getRequest())
#set ($objThemeDisplay = $httpServletRequest.getAttribute("THEME_DISPLAY"))

See the 3rd to last comment at http://www.liferay.com/web/raymond.auge/blog/-/blogs/custom-velocity-tools-and-liferay-6-0 . This works in 6.1.1 CE . Thanks Ray!
thumbnail
Jacques Traore, modificado 10 Anos atrás.

RE: Current URL in velocity variable

Junior Member Postagens: 49 Data de Entrada: 21/01/13 Postagens Recentes
Thanks a lot Henry K,
It works very fine !!!
test test, modificado 8 Anos atrás.

RE: Current URL in velocity variable

New Member Mensagem: 1 Data de Entrada: 06/03/14 Postagens Recentes
absolute URL can also retrieved as follows:

$portalUtil.getCurrentCompleteURL($request)
thumbnail
Vishal Kumar, modificado 8 Anos atrás.

RE: Current URL in velocity variable

Regular Member Postagens: 198 Data de Entrada: 12/12/12 Postagens Recentes
Praneeth T:
$portalUtil.getCurrentCompleteURL($request)



Great!!!
thumbnail
Jignesh Vachhani, modificado 11 Anos atrás.

RE: Current URL in velocity variable

Liferay Master Postagens: 803 Data de Entrada: 10/03/08 Postagens Recentes
You can append it using:
$theme_display.getURLPortal() $theme_display.getURLCurrent()

HTH,
Liferay Solutions
Aryan sds, modificado 10 Anos atrás.

RE: Current URL in velocity variable

Junior Member Postagens: 57 Data de Entrada: 24/04/12 Postagens Recentes
Hi Jignesh ,

Using your way I am getting exactly my current page url, Now how can I check whether this url contents some parameters are present or not

like my url may contains parameters like &customerName="sds"

So how can I check this whether it contents this parameter with given value or not?

Thanks,
Pravin
thumbnail
chirag @ India, modificado 10 Anos atrás.

RE: Current URL in velocity variable

Regular Member Postagens: 129 Data de Entrada: 21/12/11 Postagens Recentes
Hi Aryan sds ,

You can get parameter value using below line in velocity template and after You can used if condition for further operation.

I gets email value here..
#set($isemail = $request.get("parameters").get("email"))


HTH
Chirag@India
Aryan sds, modificado 10 Anos atrás.

RE: Current URL in velocity variable

Junior Member Postagens: 57 Data de Entrada: 24/04/12 Postagens Recentes
Hi Chirag, thanks for quick reply.
I have tried to use your way to get value for parameter 'customerName' in this way and also printed '$customer' in this way
#set($customer= $request.get("parameters").get("customerName"))

$customer

But I am not getting anything when I am trying to see value $customer. According to my understanding if paramter 'customerName' value is null or empty it will return blank so when I am trying to print '$customer' , my result should be either blank or null but I am getting result as '$customer' string itself.


Am I written above code in correct way?

Thanks,
Aryan sds
thumbnail
chirag @ India, modificado 10 Anos atrás.

RE: Current URL in velocity variable

Regular Member Postagens: 129 Data de Entrada: 21/12/11 Postagens Recentes
Aryan sds:
Hi Chirag, thanks for quick reply.
I have tried to use your way to get value for parameter 'customerName' in this way and also printed '$customer' in this way
#set($customer= $request.get("parameters").get("customerName"))

$customer

But I am not getting anything when I am trying to see value $customer. According to my understanding if paramter 'customerName' value is null or empty it will return blank so when I am trying to print '$customer' , my result should be either blank or null but I am getting result as '$customer' string itself.


Am I written above code in correct way?

Thanks,
Aryan sds

hi Aryan ,

You are absolutely right .if parameter value null then its print your variable as it is.

HTH
Chirag@India
Aryan sds, modificado 10 Anos atrás.

RE: Current URL in velocity variable

Junior Member Postagens: 57 Data de Entrada: 24/04/12 Postagens Recentes
Hi Chirag , I am trying in this way.

#set ($url = $themeDisplay.getURLCurrent())

#if($url.contains("&amp;customerName"))

#set($customer= $url.get("parameters").get("customerName"))

$customer
#end


But I am not getting $customer value though my url contains value for customerName

My url is like :
group/people/mainHome&amp;customerName=Aryan


Why I am not getting value for $customer as Aryan ?
thumbnail
chirag @ India, modificado 10 Anos atrás.

RE: Current URL in velocity variable

Regular Member Postagens: 129 Data de Entrada: 21/12/11 Postagens Recentes
Aryan sds:
Hi Chirag , I am trying in this way.

#set ($url = $themeDisplay.getURLCurrent())

#if($url.contains("&amp;customerName"))

#set($customer= $url.get("parameters").get("customerName"))

$customer
#end


But I am not getting $customer value though my url contains value for customerName

My url is like :
group/people/mainHome&amp;customerName=Aryan


Why I am not getting value for $customer as Aryan ?


hi aryan ,

you have to use $request instead of $url because when form submits then $request object is used to get parameter.
#set($customer= $request.get("parameters").get("customerName"))

one another way

If you get $url & its print whole url then you can split that url & get particular value
#set ($current_url = $request.get("attributes").CURRENT_COMPLETE_URL) //to get complate url in velocity

#set($customervalue=$url.split("customerName=").get(1))

$customervalue


HTH
Chirag@India
Aryan sds, modificado 10 Anos atrás.

RE: Current URL in velocity variable

Junior Member Postagens: 57 Data de Entrada: 24/04/12 Postagens Recentes
Hi chirag,

Its working !!emoticon
Thanks for great help....

Aryan sds
Aryan sds, modificado 10 Anos atrás.

RE: Current URL in velocity variable

Junior Member Postagens: 57 Data de Entrada: 24/04/12 Postagens Recentes
Sometimes my url becomes like
  http://localhost:8080/web/guest/sign-in?p_p_id=45&amp;p_p_lifecycle=0&amp;_58_redirect=‌​%2Fgroup%2Femployee%2FmainForm%3FempName%3DABC


So I have decoded this url by this way.

#set($absoluteUrl= $theme_display.getURLCurrent())
#set ($test=$httpUtil.decodeURL($absoluteUrl))


So my url becomes

$test=/web/guest/sign-in?p_p_id=58&p_p_lifecycle=0&_58_redirect=/group/employee/mainForm?empName=ABC

Now I want to check again value of empName , So I am trying in this way.

[code]#set($empName= $request.getParameter("empName"))

$empName

but I am not getting value of empName .

How can I get value of empName now?
thumbnail
Jitender Soodan, modificado 10 Anos atrás.

RE: Current URL in velocity variable

New Member Postagens: 5 Data de Entrada: 17/06/11 Postagens Recentes
Hi Aryan,

After scratching my head for a while I found that if you set a variable in velocity templates in Liferay, the variables are not available for use until you restart the Liferay Service.

Try that and see if that helps.