Slide 1

Slide 1 text

Scandalbars Mark Wunsch @markwunsch

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Agenda • What is this whole templating thing all about? • Why even bother? • How did you do this crazy thing?

Slide 5

Slide 5 text

The World Wide Web Client Server HTTP

Slide 6

Slide 6 text

The World Wide Web Client Server HTTP

Slide 7

Slide 7 text

The World Wide Web Client Server HTTP

Slide 8

Slide 8 text

The World Wide Web Client Server HTTP HTML

Slide 9

Slide 9 text

The World Wide Web Client Server HTTP

Slide 10

Slide 10 text

The World Wide Web Client Server HTTP

Slide 11

Slide 11 text

The World Wide Web Client Server HTTP JSON

Slide 12

Slide 12 text

handlebarsjs.com

Slide 13

Slide 13 text

SINGLE-PAGE APPS

Slide 14

Slide 14 text

SINGLE-PAGE APPS jsappsfailinghorrifically.tumblr.com

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

Client Server HTTP

Slide 18

Slide 18 text

Client Server HTTP

Slide 19

Slide 19 text

Client Server HTTP HTML

Slide 20

Slide 20 text

Client Server HTTP HTML Accept: application/json

Slide 21

Slide 21 text

Client Server HTTP HTML Accept: application/json JSON

Slide 22

Slide 22 text

Templating

Slide 23

Slide 23 text

${brandName}
${brandName}

Slide 24

Slide 24 text

} mustache.github.io

Slide 25

Slide 25 text

scalate.fusesource.org

Slide 26

Slide 26 text

This page intentionally left blank.

Slide 27

Slide 27 text

handlebarsjs.com

Slide 28

Slide 28 text

// Template <div class="entry"> <h1>{{title}}</h1> <div class="body"> {{body}} </div> </div> // Code var source = $("#entry-template").html(); var template = Handlebars.compile(source); var context = {title: "My New Post", body: "This is my first post!"} var html = template(context); // Result

My New Post

This is my first post!

Slide 29

Slide 29 text

“Maybe I’ll just write Handlebars in Scala,” I finally said to Tom. We were at a bar at the time, and I was a bit tipsy, but there was a “yeah, sure, do that” vibe to our conversation. http://tech.gilt.com/post/59708050425/handlebars-scala-a-handlebars-for-scala

Slide 30

Slide 30 text

recursive descent into madness

Slide 31

Slide 31 text

http://zaach.github.io/jison/

Slide 32

Slide 32 text

%start root %% root : program EOF { return $1; } ; program : simpleInverse statements { $$ = new yy.ProgramNode([], $2); } | statements simpleInverse statements { $$ = new yy.ProgramNode($1, $3); } | statements simpleInverse { $$ = new yy.ProgramNode($1, []); } | statements { $$ = new yy.ProgramNode($1); } | simpleInverse { $$ = new yy.ProgramNode([], []); } | "" { $$ = new yy.ProgramNode([]); } ; statements : statement { $$ = [$1]; } | statements statement { $1.push($2); $$ = $1; } ; statement : openInverse program closeBlock { $$ = new yy.BlockNode($1, $2.inverse, $2, $3); } | openBlock program closeBlock { $$ = new yy.BlockNode($1, $2, $2.inverse, $3); } | mustache { $$ = $1; } | partial { $$ = $1; } | CONTENT { $$ = new yy.ContentNode($1); } | COMMENT { $$ = new yy.CommentNode($1); }

Slide 33

Slide 33 text

λ

Slide 34

Slide 34 text

Parser Combinators http://eprints.nottingham.ac.uk/221/1/parsing.pdf

Slide 35

Slide 35 text

abstract class Parser[+T] extends (Input) 㱺 ParseResult[T]

Slide 36

Slide 36 text

def *: Parser[List[T]] def >>[U]:(fq: (T) => Parser[U]): Parser[U] def ^^[U]:(f: (T) => U): Parser[U] def |[U>: T](q:=> Parser[U]): Parser[U] def ~[U](q:=> Parser[U]): Parser[~[T,U]] Combinators

Slide 37

Slide 37 text

def *: Parser[List[T]] def >>[U]:(fq: (T) => Parser[U]): Parser[U] def ^^[U]:(f: (T) => U): Parser[U] def |[U>: T](q:=> Parser[U]): Parser[U] def ~[U](q:=> Parser[U]): Parser[~[T,U]] Combinators

Slide 38

Slide 38 text

scala.util.parsing

Slide 39

Slide 39 text

def ident: Parser[String] // Anything that is a valid Java identifier, according the The Java Language Spec. def wholeNumber: Parser[String] // An integer, without sign or with a negative sign. def stringLiteral: Parser[String] // Double quotes (") enclosing a sequence of... implicit def regex(r: Regex): Parser[String] implicit def literal(s: String): Parser[String] scala.util.parsing.combinator.JavaTokenParsers

Slide 40

Slide 40 text

def statements = rep1(statement) def statement = { inverseBlock | block | mustache | partial | CONTENT ^^ { Content(_) } | comment }

Slide 41

Slide 41 text

def statements = rep1(statement) def statement = { inverseBlock | block | mustache | partial | CONTENT ^^ { Content(_) } | comment } statements : statement { $$ = [$1]; } | statements statement { $1.push($2); $$ = $1; } ; statement : openInverse program closeBlock { $$ = new yy.BlockNode($1, $2.inverse, $2, $3); } | openBlock program closeBlock { $$ = new yy.BlockNode($1, $2, $2.inverse, $3); } | mustache { $$ = $1; } | partial { $$ = $1; } | CONTENT { $$ = new yy.ContentNode($1); } | COMMENT { $$ = new yy.CommentNode($1); } ;

Slide 42

Slide 42 text

def mustache: Parser[Mustache] = { mustachify(pad(inMustache)) ^^ { mustacheable(_) } | mustachify("&" ~> pad(inMustache)) ^^ { mustacheable(_, true) } | mustachify("{" ~> pad(inMustache) <~ "}") ^^ { mustacheable(_, true) } }

Slide 43

Slide 43 text

// Template val template = """

Hello, my name is {{name}}. I am from {{hometown}}. I have {{kids.length}} kids:

    {{#kids}}
  • {{name}} is {{age}}
  • {{/kids}}
""" // Object object Guy { val name = "Alan" val hometown = "Somewhere, TX" val kids = Seq(Map( "name" -> "Jimmy", "age -> "12" ), Map( "name" -> "Sally", "age" -> "4" )) } // Result scala> val t = Handlebars(template) t: com.gilt.handlebars.Handlebars = com.gilt.handlebars.Handlebars@496d864e scala> t(Guy) res0: String = "

Hello, my name is Alan. I am from Somewhere, TX. I have 2 kids:

  • Jimmy is 12
  • Sally is 4
"

Slide 44

Slide 44 text

trait Handlebars { def apply[T](context: T): String }

Slide 45

Slide 45 text

JSON

Slide 46

Slide 46 text

types

Slide 47

Slide 47 text

Implicit conversions will not work in a template. Because Handlebars.scala makes heavy use of reflection. Bummer, I know. This leads me too... Handlebars.scala makes heavy use of reflection. This means that there could be unexpected behavior. Method overloading will behave in bizarre ways. There is likely a performance penalty. I'm not sophisticated enough in the arts of the JVM to know the implications of this.

Slide 48

Slide 48 text

An Existential Crisis

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

github.com/mwunsch/handlebars.scala

Slide 51

Slide 51 text

{{thanks}}