Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement widgets on PlayFramework2?

Question

How can I implement a widget (resulting from database calls) to be present on each page of the website?

Context

I have a traditionnal web application with a sidebar. The sidebar contains data such as :

  • Username,
  • Details about the account (last connection and other)
  • Personnalized message based on profile of the user.
  • And other data extracted from the database.

Solutions so far

As shown in the official documentation, it is possible to render template inside of a template. Which is an interesting feature but it is not enough. Since I need data from the database to be rendered in my widget.

That would imply :

  1. Getting all the data needed for the sidebar somewhere,
  2. Passing that data from each controller to each view,
  3. Finally in each template, call an external template with the given data.

What I would like to do is something as exists in the Symfony2 framework. I would like to be able to call a controller directly.

{% render MyController:MyActionMethod %}

How can one implement this kind of mecanism?

PS : Yes, I've searched around a bit. Found this question "similar question" : How to avoid passing parameters everywhere in play2?

like image 938
i.am.michiel Avatar asked May 23 '26 16:05

i.am.michiel


1 Answers

There are two solutions, you described first - it's passing data from the controller as some kind of object and then using tags for rendering it.

Second solution is reverse, as in Play 2 each template is just a Scala function, you can also just call some method in your controller, which will get required data and render the subtemplate.

ie. to access controllers.MyController.myMethod(Integer a, Integer b) from template use

<div>
  @MyController.myMethod(1,2) 
</div>
like image 96
biesior Avatar answered May 25 '26 23:05

biesior