Does the Play Framework support snippets?

If I want to have a common part of the user interface on several pages, such as a menu, what is the recommended way to do this?

It will contain both the template of the template and the internal controller (similar to "fragments" within the framework of LiftWeb).

I know that there is a menu module for Play, but I'm more interested in how this will be achieved in general.

+8
java playframework
source share
1 answer

There are two ways to incorporate common view code into a playback platform.

You can use the tag #{include} or the tag #{extends} .

The extends tag, as the name suggests, extends from the parent view. The extends tag is used by default in the skeleton code set by Play when creating a new application. It extends main.html. You add your code here.

Includes a tag, allows you to embed the general part of the view code in your templates at the specified point. This works almost the same as php include / require, or jsp includes work.

The problem will come when your code template also requires data or logic from the model (via the controller). If so, then you will need to use the @Before or @With notation in your controller to ensure that the common piece of controller code is executed every time. You can add any data to the renderArgs list so that it is available for use in the view.

A simple example of using renderArgs would be.

 @Before private static void commonData() { // do your logic here renderArgs.put("menu", menu); renderArgs.put("selected", selectedMenuItem); } 

the values ​​you enter in renderArgs (the menus and selected in the example) will be available exactly as if you passed them to the rendering method.

+11
source share

All Articles