Play framework template automatically imports mode

2019-03-22 00:21发布

It seems like in Play framework templates,

there is an implicit "@import models._" and "@import play.api.data.Form" because my code:

@(title: String)(myForm: Form[User])

<!DOCTYPE html>

<html>
    ....
</html>

works without having to put explicit import statements after the first line. This issue has been raised in the past: https://groups.google.com/d/msg/play-framework/7FT68jd5asU/xYF0VNySJYcJ

What other classes/objects are implicitly imported?

3条回答
迷人小祖宗
2楼-- · 2019-03-22 00:23

As of Play 2.3 I use this line in build.sbt

TwirlKeys.templateImports ++= Seq("very.long.package._", "another.package._")
查看更多
Viruses.
3楼-- · 2019-03-22 00:38

You're right, Play Framework automatically add some import statements to all templates.

You can find these "default imports" in the PlaySettings trait from Play source code : https://github.com/playframework/Play20/blob/2.1.x/framework/src/sbt-plugin/src/main/scala/PlaySettings.scala

If you need to, you can add some additional imports in the project settings defined in your Build.scala :

val main = PlayProject(appName, appVersion, appDependencies, mainLang = JAVA).settings( 
    templatesImport += "com.acme._"
)
查看更多
Animai°情兽
4楼-- · 2019-03-22 00:42

You can look up the file : https://github.com/playframework/playframework/blob/master/framework/src/sbt-plugin/src/main/scala/PlayImport.scala#L40

All those mentioned there are imported.

Additionally, templatesImport += "com.acme._" is enough in 2.2.X to import all the files from a package i.e. in the file build.sbt.

查看更多
登录 后发表回答