Message Boards

How to get request.lifecycle in a template in Liferay 7.2

Alla Sapozhnikova, modified 3 Years ago.

How to get request.lifecycle in a template in Liferay 7.2

Junior Member Posts: 81 Join Date: 3/2/12 Recent Posts
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
thumbnail
Christoph Rabel, modified 3 Years ago.

RE: How to get request.lifecycle in a template in Liferay 7.2

Liferay Legend Posts: 1554 Join Date: 9/24/09 Recent Posts
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.
Alla Sapozhnikova, modified 3 Years ago.

RE: How to get request.lifecycle in a template in Liferay 7.2

Junior Member Posts: 81 Join Date: 3/2/12 Recent Posts
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 
thumbnail
Christoph Rabel, modified 3 Years ago.

RE: How to get request.lifecycle in a template in Liferay 7.2

Liferay Legend Posts: 1554 Join Date: 9/24/09 Recent Posts
renderRequest.lifecycle should work (at least in an ADT), themeDisplay.lifecycle probably works too.
thumbnail
Olaf Kock, modified 3 Years ago.

RE: How to get request.lifecycle in a template in Liferay 7.2

Liferay Legend Posts: 6403 Join Date: 9/23/08 Recent Posts
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 ;)
thumbnail
Christoph Rabel, modified 3 Years ago.

RE: How to get request.lifecycle in a template in Liferay 7.2

Liferay Legend Posts: 1554 Join Date: 9/24/09 Recent Posts
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.
thumbnail
David H Nebinger, modified 3 Years ago.

RE: How to get request.lifecycle in a template in Liferay 7.2

Liferay Legend Posts: 14916 Join Date: 9/2/06 Recent Posts
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...
thumbnail
Olaf Kock, modified 3 Years ago.

RE: How to get request.lifecycle in a template in Liferay 7.2

Liferay Legend Posts: 6403 Join Date: 9/23/08 Recent Posts
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.
Alla Sapozhnikova, modified 3 Years ago.

RE: How to get request.lifecycle in a template in Liferay 7.2

Junior Member Posts: 81 Join Date: 3/2/12 Recent Posts
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.