Foros de discusión

how to get current url in template using freemarker in liferay

Dayanand Tiwari, modificado hace 6 años.

how to get current url in template using freemarker in liferay

Junior Member Mensajes: 91 Fecha de incorporación: 25/05/17 Mensajes recientes
how to get current url in template using freemarker in liferay.

I have tried multiple things but getting errors. Can anyone please provide me that working code to get the current url in template.
Mainly I have to compare where this url contains a particular string or not in my application.
thumbnail
Massimo Bevilacqua, modificado hace 6 años.

RE: how to get current url in template using freemarker in liferay

Regular Member Mensajes: 210 Fecha de incorporación: 27/12/16 Mensajes recientes
Hi,

try this:
In order to use "staticUtil" you must delete the variable restiriction

<#assign serviceContext = staticUtil["com.liferay.portal.kernel.service.ServiceContextThreadLocal"].getServiceContext()>
<#assign themeDisplay = serviceContext.getThemeDisplay() />
<div id="CurrentUrl">
CurrentURL:  &lt;#assign currentUrl = themeDisplay.getPortalURL() + themeDisplay.getURLCurrent() /&gt;
${currentUrl}
</div>

&lt;#if currentUrl?contains("yourString")&gt;
It contains "yourString"
&lt;#else&gt;
It doesn't 'contains "yourString"
<!--#if-->


(this is for liferay 7, if you are using liferay 6 use "com.liferay.portal.service.ServiceContextThreadLocal")
Dayanand Tiwari, modificado hace 6 años.

RE: how to get current url in template using freemarker in liferay

Junior Member Mensajes: 91 Fecha de incorporación: 25/05/17 Mensajes recientes
It fails in the first line saying evoluated to null or missing..and i think themeDisplay object also not available in template script.
thumbnail
Massimo Bevilacqua, modificado hace 6 años.

RE: how to get current url in template using freemarker in liferay

Regular Member Mensajes: 210 Fecha de incorporación: 27/12/16 Mensajes recientes
It works,
maybe you missed this
In order to use "staticUtil" you must delete the variable restiriction

or this:
(this is for liferay 7, if you are using liferay 6 use "com.liferay.portal.service.ServiceContextThreadLocal")


A server restart it could be necessary.

ThemeDisplay is available retrived in this way.

You will find below the output of the previously code pasted in a simple template

thumbnail
Goran Marinkovic, modificado hace 5 años.

RE: how to get current url in template using freemarker in liferay

New Member Mensajes: 12 Fecha de incorporación: 27/03/18 Mensajes recientes
if your url bla bla like: web-serbia/gest/organization

I
<h1>${themeDisplay.getURLCurrent()}</h1> you get : web-serbia/gest/organization


II

<h2>${themeDisplay.getLayout().getFriendlyURL()}</h2> you get: /organization



III
<h3>${"${themeDisplay.getLayout().getFriendlyURL()}"?remove_beginning("/")}</h3> you get: organization
Roshan Qureshi, modificado hace 4 años.

RE: how to get current url in template using freemarker in liferay

Regular Member Mensajes: 159 Fecha de incorporación: 24/08/10 Mensajes recientes
I have same web content on two pages. I used above but getting same url on both pages.
If I do any changes in template and try from the home page its giving me  home page url
But when go to another page projects - it also gives home page url only.

If I again change something in template and visit first projects page -it gives me project url but after that home page also gives project url.
It seems some bug.