Twirl templates can be generated as a class rather than a static object by declaring a constructor using a special @this(args) syntax at the top of the template. This means that Twirl templates can be injected into controllers directly and can manage their own dependencies, rather than the controller having to manage dependencies not only for itself, but also for the templates it has to render.

As an example, suppose a template has a dependency on a component Summarizer, which is not used by the controller:

public interface Summarizer {
/** Provide short form of string if over a certain length */
String summarize(String item);
}

Create a file app/views/IndexTemplate.scala.html using the @this syntax for the constructor: