Twirl templates cannot be seen in the code (spray application)
Asked Answered
L

2

14

I have some configuration problem I can't see. I've followed the instructions provided in the latest twirl README, but the html package is undefined according to the compiler.

  1. I've included the sbt-twirl plugin in the project/plugins.sbt file

    addSbtPlugin("com.typesafe.sbt" % "sbt-twirl" % "1.0.3")
    
  2. In the project/Build.scala I've enabled the plugin

     lazy val root  = Project(id = "proj", base = file("."))
       .enablePlugins(play.twirl.sbt.SbtTwirl)
       .settings(rootSettings: _*)
    
  3. I've placed the page.scala.html in the src/main/twirl subdirectory (either directly or using the com/somethin/cool path)

Now if I'm trying to import the html package (or com.somethin.cool.html) the compiler complains it's undefined. I can compile the templates with 'twirlCompile' command, and they get properly generated in the target subdirectory, but they are invisible to the main project.

Note: This is not a PlayFramework project

  • Scala: 2.10.4
  • sbt: 0.13.6
  • twirl: 1.0.3
Lisette answered 19/11, 2014 at 1:20 Comment(0)
B
11

I similarly use Twirl in a non-Play project.

If you placed the file in src/main/com/somethin/cool then you should expect the generated class to be found at com.somethin.cool.html.page

If you placed the file in src/main/twirl/ then you should expect the generated class to be found at twirl.html.page

The Twirl compiler looks at the files it finds and puts the "html" (extension really, it could be "txt" or "json" or ...) directory underneath the longest common sub-path of the files. So, say you had:

src/main/com/somethin/cool/page1.scala.html
src/main/twirl/page2.scala.html

Then the common longest sub-path of the files is src/main (i.e. the root of the classpath). You're then going to get these classes at:

html.com.somethin/cool/page1
html.twirl.page2

Now, to address the "compiler complains it's undefined" part: you could mean several things by this.

If you mean the actual Scala Compiler being run by sbt doesn't find it, I've never had a problem with that. The generated source and .class files are in the path under target/scala_2.10/twirl/main. In that case, you might want to be more clear about the compiler's complaint and your compilation classpath.

But, if you mean that your IDE doesn't recognize the source file, you'll need to add the directory target/scala_2.10/twirl/main as one of the source directories used by your project.

One other issue I have with Twirl usage is that the IntelliJ IDEA 14 IDE doesn't really support the twirl compiler, at least not outside a Play project. Whenever I update a Twirl template, I invariably have to compile it with sbt instead of IntelliJ IDEA's built-in compiler. The JetBrains people tell me they are working on a fix for this (I've logged an issue with them about it.)

UPDATE(12/14/14): The JetBrains issue I logged is at this location. The test case for it is here on github

Breathing answered 24/11, 2014 at 14:34 Comment(0)
C
3

it might help to explicitly add an unmanaged source directory in sbt.

unmanagedSourceDirectories in Compile += baseDirectory.value / "target" / "yourTwirlGeneratedSources" 

However, this should certainly not be necessary! I suppose when running compile the dependency on twirlCompile won't be resolved either and no updated sources are generated from your twirl files?

Have you tried using previous combinations of twirl + sbt?

Cucurbit answered 22/11, 2014 at 10:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.