I'm working on a small MVC "framework" (it's really very small) in Scala. I'd like to be able to write my view files as Scala code so I can get lots of help from the compiler. Pre-compiling is great, but what I really want is a way to have the servlet container automatically compile certain files (my view files) on request so I don't have to shut down Jetty and compile all my source files at once, then start it up again just to see small changes to my HTML.
I do this a lot with .ascx files in .NET (the file will contain just one scriptlet tag with a bunch of C# code inside which writes out markup using an XmlWriter) and I love this workflow. You just make changes and then refresh your browser, but it's still getting compiled!
I don't have a lot of experience with Java, but it seems possible to do this with JSP as well. I'm wondering if this sort of thing is possible in Scala.
I have looked into building this myself (see more info here: http://www.nabble.com/Compiler-API-td12050645.html) but I would rather use something else if it's out there.
If you want something thats kinda like JSP/ASP/Erb but using Scala code you might want to take a look at Scalate.
Scalate is a Scala based template engine which allows you to use powerful Scala expressions instead of the limited JSP/JSF/JSTL EL expression language - while being completely statically typed so that templates are checked at edit/compile time for errors - and templates are reloaded on the fly as they are edited.
For JSP/ASP style of templates then try the Ssp templates in Scalate which are very JSP-like.
If you are mostly generating HTML/XML markup I'd also recommend giving the Scaml templates in Scalate a try - they are Scala versions of HAML which lead to really DRY templates
This comes up fro me when I skip JSP/frameworks by writing servlets in Scala with embedded xml for templating:
class MyServlet extends HttpServlet {
def get(req) = {
var title = "hello world"
var link = "somepage"
<html>
<head><title>{ title }</title></head>
<body><a href={ "/" + link }>Click</a></body>
</html>
}
def doGet(req: HttpServletRequest, res: HttpServletResponse) = {
val out = new PrintWriter(res.getOutputStream())
out.println(get(req))
out.close
}
}
My solution has two parts:
fsc
instead of scalac
edit
button.The constant small changes I find myself making are to the style sheet (which does not require restarting Jetty), or playing with possible HTML alternatives. The best way to do that is to right-click the HTML, click Inspect Element, then press the edit button in the firebug console, and edit it on the spot. This means no more recovering of the site's state every time you make a change.
When you get it looking right, copy the changes over to scala and hit make.
There are lots of alternatives. One alternative, for instance, is to use JRebel (formely JavaRebel), and a background compilation process on-change (such as mvn scala:cc
with Maven, for example).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With