Play framework template automatically imports models._ among other things

北战南征 提交于 2019-12-21 02:51:32

问题


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?


回答1:


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._"
)



回答2:


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

TwirlKeys.templateImports ++= Seq("very.long.package._", "another.package._")



回答3:


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.



来源:https://stackoverflow.com/questions/17397215/play-framework-template-automatically-imports-models-among-other-things

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!