4 ms·
I made a Scalatags library that lets you write your HTML templates in-line in your Scala code (or Scala.js), similar to React's JSX but without the XML syntax:
by lihaoyi 10y ago
I made a Scalatags library that lets you write your HTML templates in-line in your Scala code (or Scala.js), similar to React's JSX but without the XML syntax:
div(
h1(id:="title", "This is a title"),
p("This is a big paragraph of text")
)
http://www.lihaoyi.com/scalatags/#ScalaTags http://www.lihaoyi.com/scalatags/#ScalaTags
Although this ties it to the Scala language, it does not tie you to a particular platform: the templates can run on the JVM, in the browser with Scala.js, and even in the new scala-native LLVM backend.
The fact that the chosen language is statically typed means you get "basic" level validation right-off-the-bat thanks to the compiler:
http://www.lihaoyi.com/scalatags/#WhyScalatags http://www.lihaoyi.com/scalatags/#WhyScalatags
Basic typo-detection, enforcing things are properly nested, anti-XSS-enforcement, along with all the other "standard" IDE features: jump-to-definition for your sub-templates (which are just functions), use of arbitrary code within your templates (not unlike JS-X), etc.. Also, the fact that your templates are compiled into bytecode/JS at build time rather than interpreted and re-implementing scope management and variable-bindings and such in user-land code at runtime, means the templates are really very fast.
All this comes "for free", since your templates are code like any of the other code you write, and are handled by the IDE and optimized by the compiler in the same way.
The downside, of course, is that templates are code and thus cannot be provided safely by third parties, similar to React's JS-X