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 request.lifecycle in a template in Liferay 7.2
Hi there,
I used a template code in 6.2 in Velocity template:
#set ($portletNamespace = $request.portlet-namespace)#if ($request.lifecycle == "RENDER_PHASE")
<div id="${portletNamespace}map"><!-- --></div>
<script type="text/javascript">
AUI().use(
"aui-base", "aui-io-plugin", "aui-io-request",
function(A) {
A.io.request(
"${request.resource-url}",
{
data: {
},
dataType: "json",
on: {
success: function(event, id, obj) {
var responseData = this.get("responseData");
document.getElementById("${portletNamespace}map").innerHTML = "<i>THE RESULT:</i> " + responseData;
}
}
}
);
}
);
</script>
#elseif ($request.lifecycle == "RESOURCE_PHASE")<b>this is the resource phase result</b>#end
The code needs to get $request.lifecycle. The template worked just fine in 6.2 in Velocity.
In 7.2 $request.lifestyle is not resolved if you use Velocity, if you use FreeMaker, asking for request.lifestyle causes a syntax error.
Does anyone know how to access request.lifestyle in a template in 7.2?
Thank you.
Alla
I used a template code in 6.2 in Velocity template:
#set ($portletNamespace = $request.portlet-namespace)#if ($request.lifecycle == "RENDER_PHASE")
<div id="${portletNamespace}map"><!-- --></div>
<script type="text/javascript">
AUI().use(
"aui-base", "aui-io-plugin", "aui-io-request",
function(A) {
A.io.request(
"${request.resource-url}",
{
data: {
},
dataType: "json",
on: {
success: function(event, id, obj) {
var responseData = this.get("responseData");
document.getElementById("${portletNamespace}map").innerHTML = "<i>THE RESULT:</i> " + responseData;
}
}
}
);
}
);
</script>
#elseif ($request.lifecycle == "RESOURCE_PHASE")<b>this is the resource phase result</b>#end
The code needs to get $request.lifecycle. The template worked just fine in 6.2 in Velocity.
In 7.2 $request.lifestyle is not resolved if you use Velocity, if you use FreeMaker, asking for request.lifestyle causes a syntax error.
Does anyone know how to access request.lifestyle in a template in 7.2?
Thank you.
Alla
Since you wrote request.lifestyle three times in your post, are you sure you used request.lifecycle in your script? Note: I am not sure if that variable is there, but it should be easy to check.
Yes I am sure I used $request.lifecycle. Would you please clarify what and how I need to check? The script I posted I just copied from 6.2. It works there. And it does not work in 7.2
renderRequest.lifecycle should work (at least in an ADT), themeDisplay.lifecycle probably works too.
Christoph Rabel:
renderRequest.lifecycle should work (at least in an ADT), themeDisplay.lifecycle probably works too.
pardon my not-having-tried-ignorance... wouldn't $renderRequest.lifecycle always be "render" or the likes, while $resourceRequest.lifecycle always evaluates to "resource" or similar? If they evaluate, that is ;)
I didn't really test it either. I just looked if "lifecycle" is available. And good catch, you are right:
https://github.com/liferay/liferay-portal/blob/master/portal-impl/src/com/liferay/portlet/internal/RenderRequestImpl.java
getLifecycle is hardcoded to RENDER_PHASE in the renderRequest ... :-/
Maybe themeDisplay.lifecycle or even themeDisplay.getRequest().getLifecycle() could work? But there is also a possibility that it won't work at all with the many changes since 6.2. I mean, in the way the OP expects. It is quite possible that that hackish code by James doesn't work at all anymore.
https://github.com/liferay/liferay-portal/blob/master/portal-impl/src/com/liferay/portlet/internal/RenderRequestImpl.java
getLifecycle is hardcoded to RENDER_PHASE in the renderRequest ... :-/
Maybe themeDisplay.lifecycle or even themeDisplay.getRequest().getLifecycle() could work? But there is also a possibility that it won't work at all with the many changes since 6.2. I mean, in the way the OP expects. It is quite possible that that hackish code by James doesn't work at all anymore.
Um, I don't get it.
The fact that velocity or freemarker is running to begin with is because you are in the render phase. They certainly don't execute in the action phase nor any other phase for that matter...
The fact that velocity or freemarker is running to begin with is because you are in the render phase. They certainly don't execute in the action phase nor any other phase for that matter...
Well, templates might be evaluated during RENDER or RESOURCE requests, thus technically the question is valid.
See how James abused this technique of mashing up HTML, Velocity, Javascript and the like.
As I typically focus on what creates the best maintainable code (where best applies to maintainable), I'd make sure that nobody in the organization knows that I did it, or knows my address, once they have to maintain it. In other words: I'd look for a maintainable solution, that neatly separates the different elements a solution is built from, not for the technically possible.
That being said, James' solution is a hacky workaround for when nobody allows you to deploy server-side plugins, driving up the cost of maintenance dramatically and invisibly.
See how James abused this technique of mashing up HTML, Velocity, Javascript and the like.
As I typically focus on what creates the best maintainable code (where best applies to maintainable), I'd make sure that nobody in the organization knows that I did it, or knows my address, once they have to maintain it. In other words: I'd look for a maintainable solution, that neatly separates the different elements a solution is built from, not for the technically possible.
That being said, James' solution is a hacky workaround for when nobody allows you to deploy server-side plugins, driving up the cost of maintenance dramatically and invisibly.
Is there a way to get a liferay portlet url in a free marker/velocity web content template? And is there any documentation regarding what is available in free marker for 7.2 web content templates?Thank you very much for all your help.
Copyright © 2025 Liferay, Inc
• Privacy Policy
Powered by Liferay™