RSS

Yesod でアプリを作る 5

次に個々の記事を表示させる(View)。先程と同じように config/route の設定から始める。

/blog/#PostId                   PostViewR       GET

次に Handler/Blog.hsgetPostViewR を定義する。

getPostViewR :: PostId -> Handler RepHtml
getPostViewR postId = do
  post <- runDB $ get404 postId
  defaultLayout $ do
    setTitle $ toHtml $ postTitle post
    $(widgetFile "post")

そしてテンプレートである template/post.hamlet の作成。

<h1> #{postTitle post}
<div> #{postContent post}
<p> #{show $ postCreatedAt post}

<ul>
  <li>
    <a href=@{BlogR}>Blog Top

最後に個々の記事へのリンクを作成するために template/blog.hamlet を編集する。

<h1>Posts (/blog)</h1>
$if null posts
    <p> There are no articles in the blog
$else
    <table border="1">
      <tr><th>Name</th><th>Title</th><th>tim
estamp</th><th>Action</th>
      $forall Entity postId post <- posts
        <tr>
            <td> #{postName post}
            <td> #{postTitle post}
            <td> #{show $ postCreatedAt post}
            <td>
                <a href=@{PostViewR postId} > Show

<a href=@{PostNewR} >Post New

これで個々の記事の表示ができるようになった。