日頃の行い

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

Play Framework のテンプレートで includeする

play framework 2.0.4でincludeを行うのはどうすればいいのかなーって思ってたら簡単だったので書いておこうと思った。

Play Frameworkのドキュメント(日本語版)に書いてある関数(メソッド?)はどれを呼び出すのかなと思ってたので試してみた。

<h1>Home</h1>
 
<div id="side">
  @common.sideBar()
</div>

@common.sideBar()で呼び出されるのはここを参考にするとviewsの中のものを呼び出すらしい。
つまり
path/to/app/views/common/sideBar.scala.html
というファイルに書いてある物を呼ぶ。

path/to/app/views/common/sideBar.scala.html が

<h3>hogehoge</h3>

となっていれば

<h1>Home</h1>
 
<div id="side">
  @common.sideBar()
</div>

<h1>Home</h1>
 
<div id="side">
 <h3>hogehoge</h3>
</div>

と同じようになる。

つまり

includeは
dir_name_1.dir_name_2.filename()
で呼び出せるみたい。

ここで言うfilenameは
path/to/app/views/dir_name1/dir_name2/filename.scala.html
という関係になっているものが呼ばれる。

Scala Play Framework のmain.scala.html #3

・ controller
・ viewsの1ファイル
・ viewsのmain(テンプレート)

の関係がわかったので書いておこうと思う。

package controllers

import play.api._
import play.api.mvc._

object Application extends Controller {

  def index = Action {
    Ok(views.html.index("Your new application is ready."))
  }

}


Ok(views.html.index("Your new application is ready."))
の引数の数と

views/index.scala.html

@(message: String)

@main("Welcome to Play 2.0") {
<h1>Template</h1>
}


@(message: String)
の括弧の数は一致しなければならない。

Ok(views.html.index("Hoge","Fuga"))

なら

@(hoge_message: String)(fuga_message:String)

みたいにしないといけない。


んで、

@main("Welcome to Play 2.0") {
<h1>Template</h1>
}

の括弧の数と

views/main.scala.html

@(title: String)(main: Html)

<!DOCTYPE html>
<html>
    <head>
        <title>@title</title>
        <link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/bootstrap.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>
        <div class="container">
            <div class="hero-unit">
                @main
            </div>
        </div>
    </body>
</html>

@(title: String)(main: Html)

の括弧の数は一致しなければならない。
もし、

@main("Welcome to Play 2.0") {
<h1>Template</h1>
}("hogehoge")

にするなら

@(title: String)(main: Html)(hoge:String)

にしないといけない。

コントローラのViewを呼び出す関数の引数の数
=viewで呼び出されてるファイルの変数宣言の数

view内のmain関数の括弧の数
=mainファイルの変数宣言の数

ってなってる。んで、
変数宣言の型がStringなら( )
Htmlなら{ }
にしなければならない。

Scala Play Framework で Response 「application/json」

scala Play Framework で json形式レスポンスを返したかったから調べたら簡単だったw

app/controllers/Application.scala

object Application extends Controller {

  def index = Action {
      Ok(views.html.index("Your new application is ready."))
  }

  def jsondata  = Action {
        import play.api.libs.json._
        val json  = Seq("hoge","fuga","piyo")
        Ok(Json.toJson(json));
  }

}

conf/routes

POST / controllers.Application.jsondata

これだけで / にPOSTするとjson形式で、

f:id:arata3da4:20130215195156j:plain

って感じのが返ってくる。

responseのヘッダも調べたらなんにも設定してないのに、
「Content-Type:application/json; charset=utf-8」ってなってた。便利!

Scala Play Framework のmain.scala.html #2

ScalaフレームワークPlay Frameworkで遊んでるけど仕組みが未だによくわからないからメモを書きまくる。

app/views/main.scala.html

@(title: String)(content: Html)(footer: Html)

<!DOCTYPE html>

<html>
    <head>
        <title>@title</title>
    </head>
    <body>
    <div class="container">
    @content
    @footer
    </div> <!-- /container -->
    </body>
</html>

この時、1行目の @(title) (content) (footer)
と書かないと、下のhtml文書に変数として記述してもエラーが吐かれる。
(たしか(´・ω・`)w)

app/views/index.scala.html

@main(title="hoge"){
    <h1>Hello(from template)</h1>
}{
    <hr />
    <p class="text-info">footer</p>
}

このhtmlは main.scala.html を呼び出していて、
@main(hoge){fuga}{bar}
で、
hoge → title
fuga → content
bar → footer
が main.scala.html の宣言された変数に順番通り対応している。
(たぶん(´・ω・`)w)

上の例のように title="hoge" と変数指定して値を渡すことも出来る。
なので、以下のように新しく変数を追加したら

app/views/main.scala.html

@(title: String)(content: Html)(footer: Html)(footer_second: Html)

<!DOCTYPE html>

<html>
    <head>
        <title>@title</title>
    </head>
    <body>
    <div class="container">
    @content
    @footer
    @footer_second
    </div> <!-- /container -->
    </body>
</html>

app/views/index.scala.html

@main(title="hoge"){
    <h1>Hello(from template)</h1>
}{
    <hr />
    <p class="text-info">footer</p>
}{
    <p>ここにfooter_second</p>
}

となるはず。
(たぶん(´・ω・`))