Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
RE: how to get current url in template using freemarker in liferay
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.
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.
Massimo Bevilacqua, modified 8 Years ago.
Regular Member
Posts: 210
Join Date: 12/27/16
Recent Posts
Hi,
try this:
In order to use "staticUtil" you must delete the variable restiriction
(this is for liferay 7, if you are using liferay 6 use "com.liferay.portal.service.ServiceContextThreadLocal")
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: <#assign currentUrl = themeDisplay.getPortalURL() + themeDisplay.getURLCurrent() />
${currentUrl}
</div>
<#if currentUrl?contains("yourString")>
It contains "yourString"
<#else>
It doesn't 'contains "yourString"
<!--#if-->
(this is for liferay 7, if you are using liferay 6 use "com.liferay.portal.service.ServiceContextThreadLocal")
It fails in the first line saying evoluated to null or missing..and i think themeDisplay object also not available in template script.
Massimo Bevilacqua, modified 7 Years ago.
Regular Member
Posts: 210
Join Date: 12/27/16
Recent Posts
It works,
maybe you missed this
or this:
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
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

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
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
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.
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.
Copyright © 2025 Liferay, Inc
• Privacy Policy
Powered by Liferay™