Coming from a background in PHP, I am used to the idea that I can simply embed server-side scripts into my HTML pages. Searching online, I haven't been able to find a simple echo <h1>Hello World</h1>;
equivalent in Scala besides using a web framework like Lift. Can I do this with Scala?
See Scalate and PlayFramework
Scalate Scaml is very close to what you want
Scalate supports the following template formats
PHP is an exception in being a language designed to be embedded in an HTML page. Apache has mod_php
to do this for you. Scala, like many other languages, requires you to start up a web server to respond to requests and render HTML. You can use frameworks like Lift or Play, or you can make a Servlet-based app and deploy it into any Servlet container.
You could set up an Apache server to run CGI scripts and then insert a .scala file to be run by the scala
command - but it would be incredibly slow as it would have to start up the Java VM every time. I wouldn't recommend that for anything other than scratching a mental itch.
Here's how to generate an HTML template with SSP.
Define an object that the template will access for data.
object FunStuff {
val dinnertime = "eating stuff!"
}
Create a template:
<% import mrpowers.scalate.example.FunStuff %>
<p>
My message is "<%= List("hi", "there", "reader!").mkString(" ") %>"
At dinnertime, I like <%= FunStuff.dinnertime %>
</p>
Render the template:
val sourceDataPath = new java.io.File("./src/test/resources/simple_example.ssp").getCanonicalPath
val engine = new TemplateEngine
println(engine.layout(sourceDataPath))
Here's the result:
<p>
My message is "hi there reader!"
At dinnertime, I like eating stuff!
</p>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.