I'm looking to properly factor my views, to not generate much duplication. I'm using the Play framework, and the elements I'd like to factor out are basic common ones - headers, footers, navigation elements.

What is the standard pattern for this type of factoring? When I read about the view inheritance features, they sound as if they don't allow for reuse of components.

I'm working with version 2.0 of Play.

Say, for example, that I have a common layout for multiple pages, and I'm looking to have swappable bits - say, the central content bit, and the footer, where different pages I'm displaying have different combinations of footer and content.

2 Answers
2

Layoutwraps current view, so it's best place for starting and ending the HTML structure, adding scripts into head section of the hTML doc, and placing typical elements - such as stable header with logo, footer with copyrights etc. If you have the same main navigation on all pages, you can put it in layout too, then you can add params to the layout view to make it possible easily changing CSS classes for the active item. Note, that if you have for an example 2 different versions of main nav (for an example, one for common users, second for administrators only), you can just create 2 different layouts or use one layout and display two different versions depending on some param passed from the initial view.

Tags are used for injecting code into current view (so they has just opposite role than layouts). They are a common views without layout, and are functions with params too. They are used mainly for placing things that are changing on every page or even request - for an example notice boxes (from the doc: success, error, etc). Also they are useful for subnavigation, especially, when it's changes on every 'branch' from main navigation and it's possible, that on some pages it should not be displayed at all.

You can also use tags in the layouts (and probably, vice versa) so as you can see, there are many possibilities :)

Note: palako's sample illustrates it quite well, but it's typical for Play 1.x templating engine, for Play 2.x check referenced documentation and/or sample application delivered with the source of Play.

There's two things that I can think of. The first one is when you have the skeleton of a page and you want each of your views to fill in one of the parts of that skeleton, for example, this would be your skeleton:

These samples are for Play 1.x templating system.
–
biesiorDec 18 '12 at 10:26

ah, yes. I haven't used the 2.x yet. Is this part ALSO non backwards compatible?
–
palakoDec 18 '12 at 10:29

Yes, it's also not compatible, Play 2.0 delivers own Scala based templating engine, which is fully typed and probably is much faster than this one from Play 1 as it's compiled.
–
biesiorDec 18 '12 at 10:32