• Stars
    star
    542
  • Rank 81,982 (Top 2 %)
  • Language
    Scala
  • License
    Apache License 2.0
  • Created about 11 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Twirl is Play's default template engine

Twirl

Twitter Follow Discord GitHub Discussions StackOverflow YouTube Twitch Status OpenCollective

Build Status Maven Repository size Scala Steward badge Mergify Status

Twirl is the Play template engine.

Twirl is automatically available in Play projects and can also be used stand-alone without any dependency on Play.

See the Play documentation for the template engine for more information about the template syntax.

sbt-twirl

Twirl can also be used outside of Play. An sbt plugin is provided for easy integration with Scala or Java projects.

sbt-twirl requires sbt 1.3.0 or higher.

To add the sbt plugin to your project add the sbt plugin dependency in project/plugins.sbt:

// twirl 2.0 and newer:
addSbtPlugin("org.playframework.twirl" % "sbt-twirl" % "LATEST_VERSION")
// twirl 1.6:
addSbtPlugin("com.typesafe.play" % "sbt-twirl" % "1.6.1")
// twirl 1.5.1 and before:
addSbtPlugin("com.typesafe.sbt" % "sbt-twirl" % "1.5.1")

Replacing the LATEST_VERSION with the latest version published, which should be Latest version. And enable the plugin on projects using:

someProject.enablePlugins(SbtTwirl)

If you only have a single project and are using a build.sbt file, create a root project and enable the twirl plugin like this:

lazy val root = (project in file(".")).enablePlugins(SbtTwirl)

Template files

Twirl template files are expected to be placed under src/main/twirl or src/test/twirl, similar to scala or java sources. The source locations for template files can be configured.

Template files must be named {name}.scala.{ext} where ext can be html, js, xml, or txt.

The Twirl template compiler is automatically added as a source generator for both the main/compile and test configurations. When you run compile or Test/compile the Twirl compiler will generate Scala source files from the templates and then these Scala sources will be compiled along with the rest of your project.

Additional imports

To add additional imports for the Scala code in template files, use the templateImports key. For example:

TwirlKeys.templateImports += "org.example._"

Source directories

To configure the source directories where template files will be found, use the compileTemplates / sourceDirectories key. For example, to have template sources alongside Scala or Java source files:

Compile / TwirlKeys.compileTemplates / sourceDirectories := (Compile / unmanagedSourceDirectories).value

maven-twirl

To use the Twirl plugin in your project add the Maven plugin and Twirl API as a dependency into pom.xml:

<dependencies>
    <dependency>
        <groupId>org.playframework.twirl</groupId>
        <artifactId>twirl-api_${SCALA_VERSION}</artifactId>
        <version>${TWIRL_VERSION}</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.playframework.twirl</groupId>
            <artifactId>twirl-maven-plugin_${SCALA_VERSION}</artifactId>
            <version>${TWIRL_VERSION}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Replacing the TWIRL_VERSION with the latest version published, which should be Latest version.

Template files

Twirl template files are expected to be placed under src/main/twirl or src/test/twirl, similar to scala or java sources. The additional source locations for template files can be configured.

Template files must be named {name}.scala.{ext} where ext can be html, js, xml, or txt.

Additional imports

To add additional imports for the Scala code in template files, use the templateImports parameter. For example:

<plugin>
    <groupId>org.playframework.twirl</groupId>
    <artifactId>twirl-maven-plugin_${SCALA_VERSION}</artifactId>
    <version>${TWIRL_VERSION}</version>
    <configuration>
        <templateImports>
            <import>org.example._</import>
        </templateImports>
    </configuration>
</plugin>

Source directories

To configure the source directories where template files will be found, use the sourceDir parameter. For example:

<plugin>
    <groupId>org.playframework.twirl</groupId>
    <artifactId>twirl-maven-plugin_${SCALA_VERSION}</artifactId>
    <version>${TWIRL_VERSION}</version>
    <configuration>
        <sourceDir>${project.basedir}/src/main/templates</sourceDir>
    </configuration>
    <executions>
        <execution>
            <id>additional-source-directory</id>
            <goals>
                <goal>compile</goal>
            </goals>
            <configuration>
                <sourceDir>${project.basedir}/src/main/other-templates</sourceDir>
            </configuration>
        </execution>
    </executions>
</plugin>

Scala version

To configure the Scala version just use the suffix in artifactId.

Other properties

Also, you can use the next parameters:

<plugin>
    <groupId>org.playframework.twirl</groupId>
    <artifactId>twirl-maven-plugin_${SCALA_VERSION}</artifactId>
    <version>${TWIRL_VERSION}</version>
    <configuration>
        <constructorAnnotations>
            <annotation>@org.example.MyAnnotation()</annotation>
        </constructorAnnotations>
        <templateFormats>
            <csv>play.twirl.api.TxtFormat</csv>
        </templateFormats>
        <sourceEncoding>UTF-8</sourceEncoding>
    </configuration>
</plugin>

Snapshots

To use a snapshot version add the Sonatype Snapshot repository into pom.xml:

<pluginRepositories>
    <pluginRepository>
        <id>sonatype-snapshots</id>
        <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
        <releases>
            <enabled>false</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>

gradle-twirl

⚠️ org.playframework.twirl plugin requires Gradle 7.1 or higher.

To use the Twirl plugin in your project add the gradle plugin and Twirl API as a dependency into build.gradle.kts:

plugins {
  ...
  id("org.playframework.twirl") version "LATEST_VERSION"
}

dependencies {
  implementation("org.playframework.twirl", "twirl-api_${scalaVersion}", "LATEST_VERSION")
}

Replacing the LATEST_VERSION with the latest version published, which should be Latest version.

Template files

Twirl template files are expected to be placed under src/main/twirl or src/test/twirl, similar to scala or java sources. The additional source locations for template files can be configured.

Template files must be named {name}.scala.{ext} where ext can be html, js, xml, or txt.

Additional imports

To add additional imports for the Scala code in template files, use the templateImports key. For example:

sourceSets {
  main {
    twirl {
      templateImports.add("org.example._")
    }
  }
}

Source directories

To configure the source directories where template files will be found, use the srcDir method for SourceDirectorySet. For example:

sourceSets {
  main {
    twirl {
      srcDir("app")
    }
  }
}

Scala version

To configure the Scala version use the scalaVersion property of TwirlExtension (2.13 by default). For example:

twirl {
  scalaVersion.set("3")
}

Other properties

Also, you can use the next properties:

sourceSets {
  main {
    twirl {
      // Annotations added to constructors in injectable templates
      constructorAnnotations.add("@org.example.MyAnnotation()")
      // Defined custom twirl template formats
      templateFormats.put("csv", "play.twirl.api.TxtFormat")
      // Source encoding for template files and generated scala files
      sourceEncoding.set("<enc>")
    }
  }
}

Snapshots

To use a snapshot version add the Sonatype Snapshot repository into settings.gradle.kts:

pluginManagement {
  repositories {
    maven {
      url = uri("https://oss.sonatype.org/content/repositories/snapshots")
    }
  }
}

Releasing a new version

See https://github.com/playframework/.github/blob/main/RELEASING.md

Credits

The name twirl was thought up by the Spray team and refers to the magic @ character in the template language, which is sometimes called "twirl".

The first stand-alone version of Twirl was created by the Spray team.

An optimized version of the Twirl parser was contributed by the Scala IDE team.

More Repositories

1

playframework

The Community Maintained High Velocity Web Framework For Java and Scala.
Scala
12,527
star
2

play1

Play framework
Java
1,580
star
3

play-slick

Slick Plugin for Play
Scala
803
star
4

play-samples

Play Framework Sample Projects
JavaScript
525
star
5

play-plugins

CachePlugin
JavaScript
444
star
6

play-json

The Play JSON library
Scala
359
star
7

play-mailer

Play mailer plugin
Scala
250
star
8

anorm

The Anorm database library
Scala
237
star
9

play-scala-starter-example

Play Scala Starter Template (ideal for new users!)
CSS
236
star
10

play-ws

Standalone Play WS, an async HTTP client with fluent API
Scala
222
star
11

play-scala-rest-api-example

Example Play Scala application showing REST API
Scala
213
star
12

play-scala-react-seed

❄️ Scala Play + React seed project with full-fledged build process
Shell
203
star
13

play-scala-websocket-example

Example Play Scala application showing WebSocket use with Akka actors
Scala
195
star
14

play-java-starter-example

Play starter project in Java (ideal for new users!)
CSS
161
star
15

play-scala-isolated-slick-example

Example Play Slick Project
Scala
154
star
16

netty-reactive-streams

Reactive streams implementation for Netty.
Java
113
star
17

scalatestplus-play

ScalaTest + Play
Scala
110
star
18

play-ebean

Play Ebean module
Java
103
star
19

play-socket.io

Play socket.io support
Scala
93
star
20

play-java-websocket-example

Example Play Java application showing Websocket usage with Akka actors
Java
88
star
21

play-scala-angular-seed

πŸ€ Scala Play 2.7.x + Angular 8 with Angular CLI seed project with full-fledged build process
TypeScript
84
star
22

prune

Performance testing tool for Play Framework
Scala
81
star
23

play-silhouette

Silhouette is an authentication library for Play Framework applications that supports several authentication methods, including OAuth1, OAuth2, OpenID, CAS, 2FA, TOTP, Credentials, Basic Authentication or custom authentication schemes.
Scala
78
star
24

play-scala-seed.g8

Play Scala Seed Template: run "sbt new playframework/play-scala-seed.g8"
Scala
74
star
25

play-scala-slick-example

Example Play Scala project with Slick
Scala
59
star
26

modules.playframework.org

Java
57
star
27

play-scala-secure-session-example

An example Play application showing encrypted session management
Scala
56
star
28

play-java-ebean-example

Example Play application showing Java with Ebean
Java
53
star
29

play-java-angular-seed

🍁 Java Play 2.7.x + Angular 8 with Angular CLI seed project with full-fledged build process
TypeScript
53
star
30

play-scala-macwire-di-example

Sample project for compile-time DI with Macwire
Scala
47
star
31

play-java-rest-api-example

REST API using Play in Java
Java
45
star
32

play-scala-chatroom-example

Play chatroom with Scala API
Scala
44
star
33

play-scala-tls-example

A Play application using HTTPS and WS with optional client authentication
Scala
44
star
34

play-java-react-seed

πŸŒ€ Java Play 2.7.x + React seed project with full-fledged build process
Shell
44
star
35

playframework.com

The Play Framework website
Scala
43
star
36

play-scala-streaming-example

Example Play application showing Comet and Server Sent Events in Scala
Scala
43
star
37

play-scala-anorm-example

Example Play Database Application using Anorm
Scala
41
star
38

play-java-dagger2-example

Play Application using Dagger 2 for Compile Time DI
Java
40
star
39

play-scala-compile-di-example

Example Play Project using compile time dependency injection and Play WS with ScalaTest
Scala
38
star
40

play-soap

Play SOAP support
Scala
35
star
41

play-grpc

Play + Pekko gRPC
Scala
35
star
42

play-java-jpa-example

Example Play Java project with JPA database integration
Java
35
star
43

play-java-chatroom-example

Example Chatroom with Java API
Java
34
star
44

play-scala-fileupload-example

An example Play application showing custom multiform fileupload in Scala
Scala
29
star
45

play-java-seed.g8

Play Java Seed template: use "sbt new playframework/play-java-seed.g8"
Java
23
star
46

play-doc

Play documentation rendering support
Scala
21
star
47

play-webgoat

A vulnerable Play application for attackers.
Scala
18
star
48

play-iteratees

Scala
18
star
49

play-scala-forms-example

Example Play Project showing form processing
Scala
16
star
50

play-glassfish

Play container for Glassfish
Java
16
star
51

cachecontrol

Minimal HTTP cache management library in Scala
Scala
14
star
52

play-file-watch

This is the Play File Watch library
Java
14
star
53

play-enhancer

Java
14
star
54

play-scala-log4j2-example

An example Play project using Log4J 2 as the logging engine
Scala
14
star
55

play-java-fileupload-example

An example Play application showing custom multiform fileupload in Java
Java
14
star
56

interplay

Common sbt plugins for Play modules
Scala
12
star
57

play-generated-docs

Generated docs for publishing to the Play website.
11
star
58

play-scala-hello-world-tutorial

Hello World Tutorial for Play in Scala
HTML
10
star
59

play-spring-loader

An application loader for Play that uses Spring as the dependency injection framework
Scala
10
star
60

play-java-streaming-example

Example Play application showing Comet and Server Sent Events in Java
JavaScript
9
star
61

play-scala-grpc-example

Example for akka-grpc services embedded in Play framework applications (Scala)
CSS
9
star
62

play-meta

Team management & cross-repository effort tracking
Shell
8
star
63

play-java-hello-world-tutorial

Play Hello World tutorial in Java
HTML
6
star
64

omnidoc

Play aggregated documentation
Scala
6
star
65

.github

Scala
5
star
66

play-java-forms-example

Play Project Template showing Java Forms Processing
Java
5
star
67

play-java-compile-di-example

Example Play application using compile time DI with Java API
Java
4
star
68

play-java-grpc-example

Example for akka-grpc services embedded in Play framework applications (Java)
CSS
4
star
69

templatecontrol

Template Control for Play templates and other examples
Scala
4
star
70

play-quota-scala-example

An example of User Quotas using Play and Scala
Scala
3
star
71

modules.playframework.com

The Play Framework module index
Scala
3
star
72

play-native-loader

Loading native libraries in Play Framework
Java
3
star
73

play-quota-java-example

An example application using Play Quota with Java API
Java
3
star
74

playframework.github.io

2
star
75

jnotify

Scala
2
star
76

play-courses

Courses on play using the course management tools
CSS
1
star
77

play-antora-ui

CSS
1
star