日頃の行い

個人的な日頃の行いをつらつら書いてます\\\\ ٩( 'ω' )و ////

Scala Play Framework のmain.scala.html #1

main.scala.htmlとindex.scala.htmlってなんで2つも存在してるのだろうと思ってた。

main.scala.html

@(title: String)(content: Html)

<!DOCTYPE html>

<html>
    <head>
        <title>@title</title>
        <link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")">
        <link rel="shortcut icon" type="image/png" href="@routes.Assets.at("images/favicon.png")">
        <script src="@routes.Assets.at("javascripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
    </head>
    <body>
        @content
    </body>
</html>

これを拡張して他のviewを作成することが出来るらしい。
個人的には、php twig のextendsと同じ感じなのかな。

使い方は

ビュー:hello.scala.html

@main(title="TEST"){
    <p>Hello, @name!(from template)</p>
}

とすれば、以下の条件下で、

ルーティング:routes

GET /hello/:name controllers.Application.hello(name:String)

コントローラー:app/controllers/Application.scala

・・・

  def hello(name:String)     = Action {
      Ok(views.html.hello(name:String))
  }

・・・

/hello/myname にアクセス

<!DOCTYPE html>

<html>
    <head>
        <title>TEST</title>
        <link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")">
        <link rel="shortcut icon" type="image/png" href="@routes.Assets.at("images/favicon.png")">
        <script src="@routes.Assets.at("javascripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
    </head>
    <body>
        <p>Hello, myname!(from template)</p>
    </body>
</html>

ってなる。
こんな記述じゃ見辛いな・・・

ただ、なんで@main(){}の@contentが中括弧内{}に記述してるのかわからない(´・ω・`)

scalaの勉強しないといかんかなー