Playframework twirl Templates in multimode project

114 views
Skip to first unread message

Luis Dipotet

unread,
Jul 25, 2017, 8:26:40 PM7/25/17
to Play Framework
Hi, 
I have a multi-module Play project in which one module is for scala and the other is a play module.
In the play module i am using 2 twirl templates one of them with the built-in format (Html) and other with a  Custom Format .

The built-in template(Html) works but the Custom format doesn’t, there is any problem with the fact that be in a multi-module structure ? 
Because I've the same Custom Format in single project and it doesn't has any problem.

I’ll appreciate any help before try to use other template system.

thanks in advance 

some ref. :

error sbt lines :

info] Compiling 5 Scala sources to /Users/ldipotet/scala/scalaplay/modules/dbmodule/target/scala-2.11/classes...
[info] Compiling 22 Scala sources and 1 Java source to /Users/ldipotet/scala/scalaplay/modules/apirest/target/scala-2.11/classes...
[error] /Users/ldipotet/scala/scalaplay/modules/apirest/app/controllers/HomeController.scala:74: object csv is not a member of package views
[error]     Ok(views.csv.records(Record.sampleRecords))
[error]              ^
[error] one error found



amertum

unread,
Jul 26, 2017, 2:27:32 PM7/26/17
to Play Framework
Twirl Template files must be named {name}.scala.{ext} where ext can be htmljsxml, or txt.

amertum

unread,
Jul 26, 2017, 2:29:28 PM7/26/17
to Play Framework
OK, you've extends the format with CsvFormat.

Never mind ^^

amertum

unread,
Jul 26, 2017, 2:58:41 PM7/26/17
to Play Framework
OK, got it. You have an old use of build.sbt

You should have a build.sbt in your subproject and declare the dependencies and the twirl template import in the module apirest.

Then you need to include the import of Record and Player in your csv template.

The diff :

  1. diff --git a/modules/apirest/app/views/premier.scala.csv b/modules/apirest/app/views/premier.scala.csv
  2. index 840085c..6d3f12c 100644
  3. --- a/modules/apirest/app/views/premier.scala.csv
  4. +++ b/modules/apirest/app/views/premier.scala.csv
  5. @@ -2,5 +2,5 @@
  6.  * Template for comma separate value files *
  7.  ******************************************@
  8.  
  9. -@(matchs: List[Player])"foo","bar"@for(player <- players) {
  10. -"@player.playerName","@player.teamName"}
  11. \ No newline at end of file
  12. +@(players: List[com.ldg.model.Player])"foo","bar"@for(player <- players) {
  13. +"@player.playerName","@player.teamName"}
  14. diff --git a/modules/apirest/app/views/records.scala.csv b/modules/apirest/app/views/records.scala.csv
  15. index 7771c28..193eba2 100644
  16. --- a/modules/apirest/app/views/records.scala.csv
  17. +++ b/modules/apirest/app/views/records.scala.csv
  18. @@ -1,2 +1,2 @@
  19. -@(records: List[Record])"foo","bar"@for(r <- records) {
  20. -"@r.foo","@r.bar"}
  21. \ No newline at end of file
  22. +@(records: List[utilities.Record])"foo","bar"@for(r <- records) {
  23. +"@r.foo","@r.bar"}

amertum

unread,
Jul 26, 2017, 3:01:10 PM7/26/17
to Play Framework
And the diff about the build.sbt

  1. diff --git a/modules/apirest/build.sbt b/modules/apirest/build.sbt
  2. new file mode 100644
  3. index 0000000..21fcdb5
  4. --- /dev/null
  5. +++ b/modules/apirest/build.sbt
  6. @@ -0,0 +1,4 @@
  7. +name := """apirest"""
  8. +
  9. +TwirlKeys.templateFormats += ("csv" -> "views.CsvFormat")
  10. +

juan....@reduc.edu.cu

unread,
Jul 27, 2017, 10:47:19 AM7/27/17
to play-fr...@googlegroups.com
Hi. Amertum posted a solution that works, but there are other ways too. My
2 cents:

**1** Just moving the `TwirlKeys.templateFormats += ("csv" ->
"views.CsvFormat")` inside the `restfulapi` definition in the main
`build.sbt`, which would look like this:

...
lazy val restfulapi = (project in
file("modules/apirest")).enablePlugins(PlayScala).dependsOn(util).settings(scalaVersion:="2.11.7",
libraryDependencies ++= Seq(
cache,
"org.scalatestplus.play" %% "scalatestplus-play" % "1.5.1" % Test
),
TwirlKeys.templateFormats += ("csv" -> "views.CsvFormat")
)
...

**2** If you are going to need the custom format in other subprojects,
moving common settings to an object in a new `Common.scala` file (name it
whatever you want) in the `basedir/project` directory:

import sbt._

object Common {
val settings: Seq[Setting[_]] = Seq(
TwirlKeys.templateFormats += ("csv" -> "views.CsvFormat")
)
}

Then use Common object in subprojects `build.sbt`:

name := """apirest"""

Common.settings

See the [Play][1] and [SBT][2] documentation on subprojects for more details.

[1]: https://playframework.com/documentation/2.5.x/SBTSubProjects
[2]: https://www.scala-sbt.org/0.13/docs/Multi-Project.html

Regards,
Juan Carlos
> --
> You received this message because you are subscribed to the Google Groups
> "Play Framework" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to play-framewor...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/play-framework/1926a456-b0ce-4d94-9c46-c95fbb189c32%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>
> -----------------------------------------------------------------
> ---- Universidad de Camagüey "Ignacio Agramonte Loynaz", Cuba ---
> -----------------------------------------------------------------
> ---- https://intranet.reduc.edu.cu/ -----------------------------
> ---- https://www.reduc.edu.cu/ ----------------------------------
> -----------------------------------------------------------------
>
>


-------------------------------------------------------------------------------
---- Universidad de Camagüey "Ignacio Agramonte Loynaz", Cuba ----------------
-------------------------------------------------------------------------------
---- 6 de noviembre de 2017 ------------------------------------------------
----- Aniversario 50 de la Universidad de Camagüey "Ignacio Agramonte Loynaz"-
---- "50 años de ciencia y compromiso" ---------------------------------------
-------------------------------------------------------------------------------
---- Aniversario 95 de la FEU -------------------------------------------------
---- https://www.reduc.edu.cu/ ------------------------------------------------
-------------------------------------------------------------------------------

Luis Dipotet

unread,
Jul 28, 2017, 12:00:15 AM7/28/17
to Play Framework
Thanks a lot. It's not clear in play framework doc. It help me a lot, I was stack on it.
Reply all
Reply to author
Forward
0 new messages