How can I implement a widget (resulting from database calls) to be present on each page of the website?
I have a traditionnal web application with a sidebar. The sidebar contains data such as :
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 :
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?
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With