mwunsch / handlebars.scala

A Scala implementation of the Handlebars templating language (a superset of Mustache).
Apache License 2.0
112 stars 40 forks source link

Dot notation not working? #38

Closed tcasper closed 10 years ago

tcasper commented 10 years ago

Example in the documentation is not handling the {{kids.length}}, presumably due to the dot?

screen shot 2014-05-28 at 5 03 01 pm

chrhicks commented 10 years ago

Tried it out and it appears to work fine? Let me know if you still have this problem!

➜  handlebars.scala git:(master) sbt console
[info] Loading project definition from /Users/chicks/Code/web/handlebars.scala/project
[info] Set current project to handlebars (in build file:/Users/chicks/Code/web/handlebars.scala/)
[info] Updating {file:/Users/chicks/Code/web/handlebars.scala/}handlebars-scala...
[info] Resolving jline#jline;2.11 ...
[info] Done updating.
[info] Starting scala interpreter...
[info]
Welcome to Scala version 2.11.1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_45).
Type in expressions to have them evaluated.
Type :help for more information.

scala> val template = """
     |       <p>Hello, my name is {{name}}. I am from {{hometown}}. I have {{kids.length}} kids:</p>
     |       <ul>
     |         {{#kids}}<li>{{name}} is {{age}}</li>{{/kids}}
     |       </ul>
     |     """
template: String =
"
      <p>Hello, my name is {{name}}. I am from {{hometown}}. I have {{kids.length}} kids:</p>
      <ul>
        {{#kids}}<li>{{name}} is {{age}}</li>{{/kids}}
      </ul>
    "

scala> object Guy {
     |       val name = "Alan"
     |       val hometown = "Somewhere, TX"
     |       val kids = Seq(Map(
     |         "name" -> "Jimmy",
     |         "age" -> "12"
     |       ), Map(
     |         "name" -> "Sally",
     |         "age" -> "4"
     |       ))
     |     }
defined object Guy

scala> Guy.kids
res0: Seq[scala.collection.immutable.Map[String,String]] = List(Map(name -> Jimmy, age -> 12), Map(name -> Sally, age -> 4))

scala> Guy.kids.length
res1: Int = 2

scala> import com.gilt.handlebars.binding.dynamic._
import com.gilt.handlebars.binding.dynamic._

scala> import com.gilt.handlebars.Handlebars
import com.gilt.handlebars.Handlebars

scala> val t = Handlebars(template)
t: com.gilt.handlebars.Handlebars[Any] = com.gilt.handlebars.HandlebarsImpl@6e1ec4df

scala> t(Guy)
res2: String =
  "
      <p>Hello, my name is Alan. I am from Somewhere, TX. I have 2 kids:</p>
      <ul>
        <li>Jimmy is 12</li><li>Sally is 4</li>
      </ul>
    "