invoking other controller actions from template (page composition)

711 views
Skip to first unread message

Cyrille37

unread,
Feb 28, 2011, 4:28:06 PM2/28/11
to play-framework
Hi,

It seems that Play! does not permit to invoke another controller
action from a template. I think it's a must have behavior for a Web
Framework. We often have pages composed with several different parts
that come from different 'model domain'. For example you can render
some news in one column, last commit in another column, ... And it
will be a mistake to make all controllers aware of the complete page
composition, don't you think ?
To permit the code re-usability and cleans and simples controllers
it's important to be able to compose a page with different controller/
view.

Perhaps this behavior is available in Play! but I did not find it.

Not found in http://www.playframework.org/documentation/1.1.1/tags

Guillaume Bort said that Play! architecture is not compatible:
https://groups.google.com/group/play-framework/browse_thread/thread/3240d7d3eedbf25d

But has Japid template engine got the tag #{invoke
<controller.action> /}, it should be possible to get the behavior
natively in Play!

I stay perplex about this black hole ...
I'm sure Play! has a solution for page composition ; please, can you
light me on ;-)

Best regards
Cyrille, who learn Play!

Cyrille37

unread,
Feb 28, 2011, 4:31:20 PM2/28/11
to play-framework
Extract from the page :
https://github.com/branaway/Japid/wiki/server-side-include-with-japid

How is this different from simple include and tag invocation
An invoke is a “direct” invocation of a Play action, with its data
processing logic and page rendering of complexity of any level. The
Play’s include mechanism is a simple text replacement before
rendering. Regular Tag invocation is more sophisticated but they
usually rely on the caller to pass in data to render. In comparison,
the invoke tag offers really powerful way of page composition:
* each composite action are independent of each other, thus can be
independently tested
* the render result from each action call can be cached with a
expiration time just for this part of the content.
* it’s fast because it directly calls the action without going through
the full stack.
For any serious complex pages, the japid #{invoke} tag is the way to
go.

On 28 fév, 22:28, Cyrille37 <cyrill...@gmail.com> wrote:
> Hi,
>
> It seems that Play! does not permit to invoke another controller
> action from a template. I think it's a must have behavior for a Web
> Framework. We often have pages composed with several different parts
> that come from different 'model domain'. For example you can render
> some news in one column, last commit in another column, ... And it
> will be a mistake to make all controllers aware of the complete page
> composition, don't you think ?
> To permit the code re-usability and cleans and simples controllers
> it's important to be able to compose a page with different controller/
> view.
>
> Perhaps this behavior is available in Play! but I did not find it.
>
> Not found inhttp://www.playframework.org/documentation/1.1.1/tags
>
> Guillaume Bort said that Play! architecture is not compatible:https://groups.google.com/group/play-framework/browse_thread/thread/3...

Julien Richard-Foy

unread,
Mar 1, 2011, 3:14:53 AM3/1/11
to play-fr...@googlegroups.com, Cyrille37
A kind of “view component” is probably missing in Play! but I’m not
sure the #invoke tag of Japid is a clean workaround : in an action you
can deal with the request, its parameters, the response, the flash
scope, etc. How are these variables filled when an action is invoked
directly by Japid?

Bing Ran

unread,
Mar 1, 2011, 4:48:58 AM3/1/11
to play-fr...@googlegroups.com
Those implicit objects in controllers are byte-code engineered to be bound
to the current thread, by Play. i.e.,

when you have code like: request.xxx(), the real code is like:
Request.current(xxx)

--------------------------------------------------
From: "Julien Richard-Foy" <juli...@gmail.com>
Sent: Tuesday, March 01, 2011 4:14 PM
To: <play-fr...@googlegroups.com>
Cc: "Cyrille37" <cyri...@gmail.com>
Subject: Re: [play-framework] Re: invoking other controller actions from
template (page composition)

> A kind of �view component� is probably missing in Play! but I�m not


> sure the #invoke tag of Japid is a clean workaround : in an action you
> can deal with the request, its parameters, the response, the flash
> scope, etc. How are these variables filled when an action is invoked
> directly by Japid?
>

> --
> You received this message because you are subscribed to the Google Groups
> "play-framework" group.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to
> play-framewor...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/play-framework?hl=en.
>

Julien Tournay

unread,
Mar 1, 2011, 5:35:31 AM3/1/11
to play-fr...@googlegroups.com
Hi,

If you want to do this, you should use a component oriented framework, like wicket or tapestry.
That's not how Play works, and you should not call a controller from a template because:

 - A controller call is supposed to return a full html document, which you obviously don't want to insert in another HTML document
- Since controllers are dealing with the HTTP response, you could have tricky bugs (what if different controllers are setting the same HTTP header with different values ?)
- Play do extra stuff before and after an action invocation, like managing your DB connection. What will happen if you do multiple action invocations ?
- How do you manages @Before and @After.
- What if an action throw a Redirect ?
- etc...

jto.




On Tue, Mar 1, 2011 at 10:48 AM, Bing Ran <bing...@gmail.com> wrote:
Those implicit objects in controllers are byte-code engineered to be bound to the current thread, by Play. i.e.,

when you have code like:  request.xxx(), the real code is like: Request.current(xxx)





--------------------------------------------------
From: "Julien Richard-Foy" <juli...@gmail.com>
Sent: Tuesday, March 01, 2011 4:14 PM
To: <play-fr...@googlegroups.com>
Cc: "Cyrille37" <cyri...@gmail.com>
Subject: Re: [play-framework] Re: invoking other controller actions from template (page composition)


A kind of “view component” is probably missing in Play! but I’m not

sure the #invoke tag of Japid is a clean workaround : in an action you
can deal with the request, its parameters, the response, the flash
scope, etc. How are these variables filled when an action is invoked
directly by Japid?

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.


--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.




--
Real Programmers don't need comments-- the code is obvious.

Bing Ran

unread,
Mar 1, 2011, 6:28:29 AM3/1/11
to play-fr...@googlegroups.com
Hi,
 
Please see comment inline.
 
Thanks.
 
Bing
 

Sent: Tuesday, March 01, 2011 6:35 PM
Subject: Re: [play-framework] Re: invoking other controller actions from template (page composition)

Hi,

If you want to do this, you should use a component oriented framework, like wicket or tapestry.
That's not how Play works, and you should not call a controller from a template because:

I'm already using it in my work and it seems to work fine without leaving the otherwise lovely Play framework.
 
 - A controller call is supposed to return a full html document, which you obviously don't want to insert in another HTML document
 
A portlet like web page is composed of multiple panels that potentially come from multiple sources.  I don't see why page composition is a bad thing. It's a quite common pattern for content rich web pages. It does not have to be built with a pure component-based framework.
 
 
- Since controllers are dealing with the HTTP response, you could have tricky bugs (what if different controllers are setting the same HTTP header with different values ?)
 
It's a policy issue that can be controlled in programs. Right now later calls will override previous ones. People can make similar mistakes if they choose to, using plain Play or not. My team has been using this pattern a lot and they haven't been bothered by that as I can recall.
 
 
- Play do extra stuff before and after an action invocation, like managing your DB connection. What will happen if you do multiple action invocations ?
 
DB connection management is done in very early and very late phases during a request/response cycle. Japid invokes are in the middle, meaning they are in the same database transaction.
 
- How do you manages @Before and @After.
 
Right now "invoke" simply invokes the action method without going through all the before/after logic. I keep this in mind when I use "invoke".
 
- What if an action throw a Redirect ?
 
Since I know a action will be used as a sort of "component", I avoid redirect in it.
 
- etc...

jto.


The point is I wouldn't dictate how a programmer should think when they adapt a framework. I would give them choice whenever I could and let make conscious decision.

Julien Richard-Foy

unread,
Mar 1, 2011, 7:00:17 AM3/1/11
to play-fr...@googlegroups.com, Bing Ran
>> - What if an action throw a Redirect ?
>
> Since I know a action will be used as a sort of "component", I avoid
> redirect in it.

So when you write an action you should be aware of the context of its call?
I agree that composing views is an interesting feature but I
definitely think that calling several actions is a bad way to achieve
it.

martin

unread,
Mar 1, 2011, 8:00:01 AM3/1/11
to play-framework
What about using Java Tags for this? I think it should work for
request independent parts like a global calendar or a shoutbox. But i
am not sure what the limitations for Java Tags are.

Bing Ran

unread,
Mar 1, 2011, 8:14:26 AM3/1/11
to play-fr...@googlegroups.com
Assuming you were talking about FastTags, yes, it's possible, but the
interface is not really user friendly:

public static void _myTag(
Map<?, ?> args,
Closure body,
PrintWriter out,
ExecutableTemplate template,
int fromLine)

You're really dealing with low level Groovy stuff with it.


--------------------------------------------------
From: "martin" <martin...@googlemail.com>
Sent: Tuesday, March 01, 2011 9:00 PM
To: "play-framework" <play-fr...@googlegroups.com>
Subject: [play-framework] Re: invoking other controller actions from
template (page composition)

> What about using Java Tags for this? I think it should work for

Julien Tournay

unread,
Mar 1, 2011, 8:20:38 AM3/1/11
to play-fr...@googlegroups.com
Hi,

- I never said that page composition is a bad thing, and I never said you can't do a "portlet like webpage" in play. I just said you should not invoke controllers from views.

- Yes people can make similar mistakes, but it's a lot easier to make mistakes if you're invoking a lot of different controllers in the same page.

- DB connection is just an example. Basically, any module could break if the controller is not invoked as expected.

- Well, ok you're not using redirect. But it doesn't means that Redirect, or anything different than an OK response won't be the result of the action call. For example the user could lack privileges to access some data, and the action would throw a redirect. Also you're supposing that the developer knows what's going on under the hood, which is really a bad thing for a framework.

When you choose a framework, you also choose a model to develop with. Invoking controllers from views just sounds like a hack to me, and play was never meant to be used that way.

You're obviously free to use that if you wish thought, but be aware that you could run into troubles.

jto.

Bing Ran

unread,
Mar 1, 2011, 10:01:51 AM3/1/11
to play-fr...@googlegroups.com
Ok, as I have replied in the previous email to another email, Japid simply uses the action as a plain old Java static method without bothering with any other logic surrounding a regular action call, including any modules and plugins, so I don't need to worry about any side effects.   Calling it a super-charged tag is pretty close.
 
It is a hack, but a hack I have enjoyed using.

Guillaume Bort

unread,
Mar 1, 2011, 12:35:23 PM3/1/11
to play-fr...@googlegroups.com
As someone pointed out in another Thread, Symfony solves this problem
by introducing a Component notion:
http://www.symfony-project.org/book/1_0/07-Inside-the-View-Layer#chapter_07_sub_components

It is good idea to keep a clear separation between controller actions
that will get invoked by an HTTP call (with the clear contract that
only one action will be called during a request), and a Component
model that help to organize your presentation logic when you need to
write more complicated code.

We'll introduce a very similar concept in a next version of Play (not
the 1.2 since I want to release it this month).

--
Guillaume Bort, http://guillaume.bort.fr

For anything work-related, use g...@zenexity.fr; for everything else,
write guillau...@gmail.com

cyrille giquello

unread,
Mar 1, 2011, 6:03:06 PM3/1/11
to play-fr...@googlegroups.com
2011/3/1 Guillaume Bort <guillau...@gmail.com>:
> ...

> It is good idea to keep a clear separation between controller actions
> that will get invoked by an HTTP call (with the clear contract that
> only one action will be called during a request), and a Component
> model that help to organize your presentation logic when you need to
> write more complicated code.
>
> We'll introduce a very similar concept in a next version of Play (not
> the 1.2 since I want to release it this month).
>

miam miam ... I'll be patient ;-)

Cheers
Cyrille

sas

unread,
Mar 1, 2011, 7:47:00 PM3/1/11
to play-framework
yeap, that would be an excellent addition

you mean that controller action will only be called from http and the
componentes would only be called from the views?

saludos

sas

On 1 mar, 14:35, Guillaume Bort <guillaume.b...@gmail.com> wrote:
> As someone pointed out in another Thread, Symfony solves this problem
> by introducing a Component notion:http://www.symfony-project.org/book/1_0/07-Inside-the-View-Layer#chap...
> >> On Tue, Mar 1, 2011 at 10:48 AM, Bing Ran <bing....@gmail.com> wrote:
>
> >>> Those implicit objects in controllers are byte-code engineered to be
> >>> bound to the current thread, by Play. i.e.,
>
> >>> when you have code like:  request.xxx(), the real code is like:
> >>> Request.current(xxx)
>
> >>> --------------------------------------------------
> >>> From: "Julien Richard-Foy" <julien...@gmail.com>
> >>> Sent: Tuesday, March 01, 2011 4:14 PM
> >>> To: <play-fr...@googlegroups.com>
> >>> Cc: "Cyrille37" <cyrill...@gmail.com>
> Guillaume Bort,http://guillaume.bort.fr
>
> For anything work-related, use g...@zenexity.fr; for everything else,
> write guillaume.b...@gmail.com

Guillaume Bort

unread,
Mar 2, 2011, 2:19:28 AM3/2/11
to play-fr...@googlegroups.com
> you mean that controller action will only be called from http and the
> componentes would only be called from the views?

Yes.

--
Guillaume Bort, http://guillaume.bort.fr

For anything work-related, use g...@zenexity.fr; for everything else,

write guillau...@gmail.com

Bing Ran

unread,
Mar 2, 2011, 2:20:59 AM3/2/11
to play-fr...@googlegroups.com
Hi,

The Symfony component model is less than impressive. I'm sure you'll come up
with something better based on technical merits instead of preconceived
notions.

Bing


--------------------------------------------------
From: "Guillaume Bort" <guillau...@gmail.com>
Sent: Wednesday, March 02, 2011 1:35 AM

>>>>> A kind of �view component� is probably missing in Play! but I�m not

A. Karimi

unread,
Oct 29, 2013, 9:14:31 AM10/29/13
to play-fr...@googlegroups.com, cyri...@gmail.com
Is there any new feature in play 2.1.x that support it now?

Ayad Al-Qershi

unread,
Apr 24, 2014, 6:41:11 AM4/24/14
to play-fr...@googlegroups.com
Hi Miroslav,

Did you introduce this feature yet?

Regards,

Iyad

On Wednesday, August 22, 2012 11:18:40 AM UTC+4, Miroslav Dubovský wrote:

>  We'll introduce a very similar concept in a next version of Play ...

  Exists this feature in Play 1.2.x now ? 

Dňa utorok, 1. marca 2011 18:35:23 UTC+1 Guillaume Bort napísal(-a):
Reply all
Reply to author
Forward
0 new messages