Using #{include tag to include non-static elements

34 views
Skip to first unread message

John Pletka

unread,
Mar 25, 2010, 1:11:52 PM3/25/10
to play-fr...@googlegroups.com
I have a dynamically generated snippit that I want to include in my
main template, but the #include does not seem to allow it to happen.

For example:
Category extends Controller{
static void categoryList(){
//do logic to build category list
render(categories);
}
}
views/Category categoryList.html:
<ul>
#{list items:categories, as:category}
...
#{/list}
</ul>


In my main template, I've tried the following combinations:
#{include @{Category.categoryList} /}
#{include "/category/categoryList" /}

The 1st has an error "include tag not terminated" and the second says
"template not found". Is there some other way of doing this that I'm
missing?

Thanks

Guillaume Bort

unread,
Mar 25, 2010, 1:27:02 PM3/25/10
to play-fr...@googlegroups.com
The #{include} is intended to include another template, not to call
another request.

You should extract your code in the model part. The controller part
must be a thin wrapper around your application functionalities.

> --
> 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.
>
>

John Pletka

unread,
Mar 25, 2010, 1:35:34 PM3/25/10
to play-fr...@googlegroups.com
So what would be the best way to include a dynamically generated
snippit in the main template? Custom tags require you to pass in the
data to them and I don't want to have to remember to fetch that data
in every single Controller action. I guess what I'm looking for is
the equivalent of <jsp:include page=""/> where it will actually run
self-contained business logic and include the generate HTML results.

I could do it via AJAX, but that seems like a messy work-around.

Guillaume Bort

unread,
Mar 25, 2010, 2:34:02 PM3/25/10
to play-fr...@googlegroups.com
You can use a factory method to build your category list, and call it
from the tag:

#{list items:models.Category.all(), ...

John Pletka

unread,
Mar 25, 2010, 4:50:46 PM3/25/10
to play-fr...@googlegroups.com
But what if it is a non-trivial amount of logic? Say for example,
show the categories the user subscribed to with a count of articles
that contain keywords he setup in his profile. Directly accessing the
model does not really solve this in the general case.

Or are you saying I should put the code to replicate the work of the
controller in my tag? Or create a helper class to do the work (which
would need access to User,Session,JPA,etc) then use that inside the
tag? It would seem easier to just be able to use the existing
framework and allow #{include to include the generated output of a
controller/action like <jsp:include> does.

Bing Ran

unread,
Mar 25, 2010, 11:17:25 PM3/25/10
to play-fr...@googlegroups.com
Hi,

I added a special tag in the Japid module exactly for use in complex pages where different part of a page is backed by a full action invocation and the render result from, with the added benefits of cache control.
 
I have found my team members naturally picked up the mechanism when they did page composition, and sometimes maybe over-used it. Overall my developers enjoy the clean organization  of pages it allows.
 
 
--
 
Bing



--------------------------------------------------
From: "John Pletka" <jpl...@abraxis.com>
Sent: Friday, March 26, 2010 4:50 AM
To: <play-fr...@googlegroups.com>
Subject: Re: [play-framework] Using #{include tag to include non-static elements

Guillaume Bort

unread,
Mar 26, 2010, 6:04:19 AM3/26/10
to play-fr...@googlegroups.com
> But what if it is a non-trivial amount of logic? Say for example,
> show the categories the user subscribed to with a count of articles
> that contain keywords he setup in his profile. Directly accessing the
> model does not really solve this in the general case.

Well the model layer should contains all this business logic,
especially the non-trivial one. The controllers should not implement
any business logic directly, especially if this logic need to be
reused in several part of the application.

Again the idea is to keep the controller and view parts as light as possible.

> It would seem easier to just be able to use the existing
> framework and allow #{include to include the generated output of a
> controller/action like <jsp:include> does.

Not really because the framework is designed to work with the HTTP
request concept. Allowing to include an HTTP request into another one
will complexify a lot the framework design.

John Pletka

unread,
Mar 26, 2010, 10:09:15 AM3/26/10
to play-fr...@googlegroups.com
The #{invoke tag was exactly what I was looking for - especially the
time-based caching feature. I don't suppose that is portable to
standard play template engine?

Bing Ran

unread,
Mar 26, 2010, 12:27:35 PM3/26/10
to play-fr...@googlegroups.com
No, it's separate from the default template engine. It can co-exist with it
though, meaning some actions use the default engine to render, others use
Japid. But the invoke tag will only work if both the caller and the callee
are in Japid.


--------------------------------------------------
From: "John Pletka" <jpl...@abraxis.com>

Sent: Friday, March 26, 2010 10:09 PM

Reply all
Reply to author
Forward
0 new messages