Tags with actions

10 views
Skip to first unread message

Marcus Downing

unread,
Sep 14, 2010, 6:44:36 AM9/14/10
to play-framework
I'd like to use tags to display some dynamic content, such as a menu
or breadcrumbs. You can include a tag like so: #{tagname /} and it
will find views/tags/tagname.html or views/tags/tagname.tag and render
it. However, they're not actions on a controller, just html templates.

You can do scripts within a tag with %{ out.println("<h1>hello</
h1>"); } but this isn't recommended. Partly because it's the wrong
place to be doing intelligent behaviour, and partly because that has
to be written in Java rather than Scala (which we prefer).

So is there a correct way of including rich behaviour from a template?

Guillaume Bort

unread,
Sep 14, 2010, 6:56:44 AM9/14/10
to play-fr...@googlegroups.com
> So is there a correct way of including rich behaviour from a template?

What is a rich behaviour? A template just display some values built by
the application model and extracted by the controller.

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

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

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

Marcus Downing

unread,
Sep 14, 2010, 7:34:20 AM9/14/10
to play-framework
For example, looking up something from the database and changing the
result depending on it. The sort of behaviour that's not recommended
in templates.

If I have the wrong approach to this, what's the right one?
> Guillaume Bort,http://guillaume.bort.fr
>
> For anything work-related, use g...@zenexity.fr; for everything else,
> write guillaume.b...@gmail.com

Arthur

unread,
Sep 14, 2010, 7:38:39 AM9/14/10
to play-framework
Are you trying to dynamically load HTML depending upon the value in a
DB? Or, are you trying to dynamically load a different HTML file
depending upon a value in the DB?

In the first case, you can write a controller to supply the necessary
HTML; I'm not so sure in the second case. Maybe you could have the
controller read in the HTML file that you want and return the contents
to the view.

~
Arthur

Marcus Downing

unread,
Sep 14, 2010, 8:40:24 AM9/14/10
to play-framework
But can I write a controller action for the tag, or do I have to get
the originating action to do it?

Perhaps I'm not explaining it very well. What I'm trying to do is have
parts of the navigation that are visible on lots of different
templates, and so common to lots of different controllers/actions,
without each of those actions having to include the furniture code.

Julien Tournay

unread,
Sep 14, 2010, 8:49:41 AM9/14/10
to play-fr...@googlegroups.com
Hum, I think I understand what you want to do.
Basically, you have a tag that needs some data computed in the Controller, but you don't want to copy the needed code in all controllers. Am I right ?

Have a look at @Before and at "renderArgs", you should find what you're searching :)

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

Guillaume Bort

unread,
Sep 14, 2010, 9:32:18 AM9/14/10
to play-fr...@googlegroups.com
Yes Play is a Controller first framework (like Django & Rails). There
is also some View first framework (like Lift). So in a Controller
first framework, each HTTP request call a single Controller action
that render a single template. All the data needed by the whole
template will be agregated by the Controller:

public static void anyAction() {
BreadCrumb bc = BreadCrumb.forAction("...");
ShoppingCart cart = getShoppingCart();
... // extract more data from your application here
render(bc, cart, ...);
}

Or like Julien advice, you can sometimes write some code only once in
a @Before filter. The important thing to remember is that you don't
write business code neither in template nor Controller.

The application model provides some data, the controller extract some
data needed for a particular request, the template display these data.

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

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

write guillau...@gmail.com

Marcus Downing

unread,
Sep 14, 2010, 9:40:00 AM9/14/10
to play-framework
I think Julien has it right with @Before - something that goes in the
controller but doesn't need repeating. At worst it would be a trait
applied to several controllers - does that work?


On Sep 14, 1:49 pm, Julien Tournay <boudhe...@gmail.com> wrote:
> Hum, I think I understand what you want to do.
> Basically, you have a tag that needs some data computed in the Controller,
> but you don't want to copy the needed code in all controllers. Am I right ?
>
> Have a look at @Before and at "renderArgs", you should find what you're
> searching :)
>
> jto
>
> > play-framewor...@googlegroups.com<play-framework%2Bunsubscribe@go oglegroups.com>
> > .
> > > > > > For more options, visit this group athttp://
> > groups.google.com/group/play-framework?hl=en.
>
> > > > > --
> > > > > Guillaume Bort,http://guillaume.bort.fr
>
> > > > > For anything work-related, use g...@zenexity.fr; for everything
> > else,
> > > > > write guillaume.b...@gmail.com
>
> > --
> > 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<play-framework%2Bunsubscribe@go oglegroups.com>
> > .

Julien Tournay

unread,
Sep 14, 2010, 9:43:06 AM9/14/10
to play-fr...@googlegroups.com
You could also use @With if you need a @Before in several controllers.

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.

Marcus Downing

unread,
Sep 15, 2010, 3:54:31 AM9/15/10
to play-framework
That looks nice. Is that the official way of mixing in controller
behaviour, rather than with traits?

On Sep 14, 2:43 pm, Julien Tournay <boudhe...@gmail.com> wrote:
> You could also use @With if you need a @Before in several controllers.
>

Julien Tournay

unread,
Sep 15, 2010, 4:28:11 AM9/15/10
to play-fr...@googlegroups.com
There's no traits in Java, so play need the @With annotation.
If you're using scala, just use traits.

jto.

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.

Reply all
Reply to author
Forward
0 new messages