例えばindex.tpl
を使いたい時。
ルーティング
router.get("/", index);
として、
fn index<'a>(req: &mut Request, res: Response<'a>) -> MiddlewareResult<'a> { let conn = Connection::connect("postgres://postgres@localhost", SslMode::None).unwrap(); let mut data = HashMap::<&str, String>::new(); return res.render("app/movie/views/index.tpl", &data); }
としてたが、空のデータ送るのは冗長。
他の方法
send_file
メソッドに変えればよい。
fn index<'a>(req: &mut Request, mut res: Response<'a>) -> MiddlewareResult<'a> { let conn = Connection::connect("postgres://postgres@localhost", SslMode::None).unwrap(); res.set(MediaType::Html); res.send_file("app/movie/views/index.tpl") }
のように、MediaType
のヘッダをつけたり、
※以下のようにMediaTypeのインポート必須
use nickel::{Nickel, Request, Response, HttpRouter, MiddlewareResult, MediaType, StaticFilesHandler,JsonBody};
set
してるのでミュータブルにするためにmut res
にしたりでできた。