Last active
December 26, 2015 20:08
-
-
Save mslinn/7205854 to your computer and use it in GitHub Desktop.
Figuring out @johanandren's answer to http://stackoverflow.com/questions/19622137/evaluate-a-string-in-a-play-framework-2-template-as-scala-code/19640185.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name := "demo" | |
version := "1.0-SNAPSHOT" | |
scalaVersion := "2.10.3" | |
version := "0.1.0" | |
play.Project.playScalaSettings | |
scalacOptions ++= Seq("-deprecation", "-encoding", "UTF-8", "-feature", "-target:jvm-1.6", "-unchecked", | |
"-Ywarn-adapted-args", "-Ywarn-value-discard", "-Xlint") | |
libraryDependencies ++= Seq( | |
jdbc, | |
anorm, | |
cache, | |
"org.webjars" %% "webjars-play" % "2.2.0", | |
"org.webjars" % "bootstrap" % "3.0.0", | |
"org.scala-lang" % "scala-compiler" % "2.10.3", | |
"org.scala-lang" % "scala-library" % "2.10.3", | |
"org.scala-lang" % "scala-reflect" % "2.10.3" | |
) | |
javaOptions in Test ++= Seq( "-Dconfig.file=conf/dev.conf" ) | |
logBuffered in Test := false | |
Keys.fork in Test := false | |
parallelExecution in Test := false | |
// define the statements initially evaluated when entering 'console', 'console-quick', or 'console-project' | |
initialCommands := """ // make app resources accessible | |
|Thread.currentThread.setContextClassLoader(getClass.getClassLoader) | |
|new play.core.StaticApplication(new java.io.File(".")) | |
|""".stripMargin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package views.helper | |
import java.io.{ByteArrayOutputStream, File, OutputStream} | |
import scala.tools.nsc.interpreter.IMain | |
import scala.tools.nsc.Settings | |
object eval { | |
val settings: Settings = { | |
lazy val urls = java.lang.Thread.currentThread.getContextClassLoader match { | |
case cl: java.net.URLClassLoader => cl.getURLs.toList | |
case _ => sys.error("classloader is not a URLClassLoader") | |
} | |
lazy val classpath = urls map {_.toString} | |
val tmp = new Settings() | |
tmp.bootclasspath.value = classpath.distinct mkString File.pathSeparator | |
tmp | |
} | |
val interpreter = new IMain(settings) | |
def apply(source: String): String = | |
stringFromStream { sout => | |
Console.withOut(sout) { | |
interpreter.interpret(source) | |
} | |
} | |
// lets someone write to a stream and then we return the string contents of that stream | |
def stringFromStream(f: OutputStream => Any): String = { | |
val out = new ByteArrayOutputStream | |
f(out) | |
out.toString("UTF-8") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Failed to initialize compiler: object scala.runtime in compiler mirror not found. | |
** Note that as of 2.8 scala does not assume use of the java classpath. | |
** For the old behavior pass -usejavacp to scala, or if using a Settings | |
** object programatically, settings.usejavacp.value = true. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@() | |
@import views.helper.eval | |
@eval("List(1,2,3).map(_*2).mkString") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have modified solution for specifying the classpath to make it work in a variety of environments. Here is what I did: