scaml
文件大小: unknow
源码售价: 5 个金币 积分规则     积分充值
资源说明:scaml
# scaml

scaml is a Scala version/dialect/rip-off/etc of HAML. It is an attempt to bring the beauty and ease of markup in HAML to a significantly different language and enviorment.

## what's it look like?

Take this simple SCAML file:

	%div
	  %h1 Test.
	  %p
	    Welcome, Mr
	    %span.bold Boldfase

This is rendered to the following Scala source file:

	package org.prohax.scaml.output

	import scala.xml._
	import org.prohax.scaml.ScamlFile

	import org.prohax.scaml.models._

	object literals extends ScamlFile[Unit] {
	  def renderXml(t:Unit) = {
	    

Test.

Welcome, Mr Boldfase

} } Then, when rendered, produces the following HTML:

Test.

Welcome, Mr Boldfase

Note that the above SCAML is valid HAML, and running it through the HAML compiler gives something virtually the same:

Test.

Welcome, Mr Boldfase

## embedded Scala SCAML allows embedding of code very similarly to HAML: %p Counting to three: %ul = (1 to 3).map(i => %li= i %p First 9 squares: %ul = (1 to 9).map { i => %li= i*i %p Nesting times: %table = List((1,2), (3,4), (5,6)).map { x => %tr %td= x._1 = (1 to x._2).map { i => %td= i This produces nice, clean Scala code. def renderXml(t:Unit) = {

Counting to three:

    { (1 to 3).map(i =>
  • { i }
  • ) }

First 9 squares:

    { (1 to 9).map { i =>
  • { i*i }
  • } }

Nesting times:

{ List((1,2), (3,4), (5,6)).map { x => { (1 to x._2).map { i => } } } }
{ x._1 }{ i }

} And the following, slightly less clean HTML.

Counting to three:

  • 1
  • 2
  • 3

First 9 squares:

  • 1
  • 4
  • 9
  • 16
  • 25
  • 36
  • 49
  • 64
  • 81

Nesting times:

1 12
3 1234
5 123456

## passing variables Usually, templates are passed (perhaps implicitly) model objects to render. Assuming we have a simple model and some instances: case class Post(author: String, body: List[String], date: Date) List( Post("Glen", List("This is line 1", "and line 2", "and line 3"), new Date(2009, 12, 28)), Post("Ben", List("Lol I am too sick for cider"), new Date(2009, 11, 28)), Post("Dylan", List("I", "like", "cider."), new Date(2009, 10, 28)) ) You write this SCAML: /! posts: List[Post] #posts %h1 Got = posts.length posts for ya. .spacer %ul = posts.map(p => %li %span.author= p.author %span.posts= p.body.take(1) %span.date= "%tD" format p.date Which renders this Scala source: object params extends ScamlFile[(List[Post])] { def renderXml(t:(List[Post])) = t match { case (posts) =>

Got { posts.length } posts for ya.

    { posts.map(p =>
  • { p.author } { p.body.take(1) } { "%tD" format p.date }
  • ) }
} And this html:

Got 3 posts for ya.

  • Glen This is line 1 01/28/10
  • Ben Lol I am too sick for cider 12/28/09
  • Dylan I 11/28/09
## so what? Well, the idea is to flesh this out until it supports as much of the HAML spec as possible, and provides a legitimate option for JVM web apps. I'd like to integrate it with [Play](http://www.playframework.org/), which I see as the most promising web framework in the world right now, for the JVM or otherwise. ## next steps Passing variables into these templates is more difficult than in HAML, since the Scala source must be compiled. I've adopted something similar to [SXT](http://github.com/nkpart/sxt), but more constrained. It will take integration with a framework for this to be really locked down. Figuring a nice way to work with the Scala branch of Play would be nice too. And deploying as a [javagem](https://www.javagems.org/) would be kickin rad. ## problems - Using the built-in Scala XHTML output gives little control over the resulting HTML, unlike HAML. - Needs to be integrated into frameworks before anyone could start using it, but that's ok because - Needs to support another ton of HAML before anyone could start using it.

本源码包内暂不包含可直接显示的源代码文件,请下载源码包。