如何在Yesod中呈现Html代数数据类型

时间:2013-06-01 12:46:05

标签: haskell rich-text-editor yesod richtext hamlet

我正在尝试在Yesod应用程序中使用CKEditor。来自CKEditor的数据通过Textarea返回给服务器,然后我将其作为Html存储在数据库中。我的问题是我知道如何在处理程序中从数据库中检索它时如何显示Html代数数据类型。我一直在阅读this教程,但它只会将Html显示为一个很长的字符串,而不是标记。

注意:titleAcontextA是我要在article-local-display中显示的变量。 contextAHtml代数数据类型

PS:我需要将Html转换为hamlet以进行渲染吗?

module Handler.Article where

import Import
import Data.Text (unpack)
import Data.Time (getCurrentTime)
import Data.String (fromString)

getArticleR :: Handler RepHtml
getArticleR = do
  defaultLayout $ do
      setTitle "Search For Article"
      $(widgetFile "header")
      $(widgetFile "article")

postArticleR :: Handler RepHtml
postArticleR = do
  redirect ArticleR

getArticleLocalR :: Handler RepHtml
getArticleLocalR = do
  articles <- runDB $ selectList ([] :: [Filter Article]) [Desc ArticleTime]
  defaultLayout $ do
      setTitle "Local Article"
      $(widgetFile "header")
      $(widgetFile "article-local")

getArticleLocalDisplayR :: ArticleId -> Handler RepHtml
getArticleLocalDisplayR articleId =  do 
  article <- runDB $ get404 articleId
  let titleA   = articleTitle article
      contextA = articleContext article
  defaultLayout $ do
      setTitle "Article"
      $(widgetFile "header")
      $(widgetFile "article-local-display")

getArticleLocalCreateR :: Handler RepHtml
getArticleLocalCreateR = do
  defaultLayout $ do
      setTitle "Create article"
      addScript $ StaticR ckeditor_ckeditor_js
      $(widgetFile "header")
      $(widgetFile "article-local-create")

postArticleLocalCreateR :: Handler RepHtml
postArticleLocalCreateR = do
  articleForm <- runInputPost $ ArticleForm <$> ireq textField "title" <*> ireq textareaField "editor1"
  now         <- liftIO getCurrentTime
  let titleA  =  title articleForm
      html    =  toHtml $ unTextarea $ context articleForm
  _           <- runDB $ insert $ Article titleA html now
  redirect ArticleLocalR

data ArticleForm = ArticleForm {
    title   :: Text,
    context :: Textarea
  }
  deriving Show

模型文件:

Article
  title Text
  context Html
  time UTCTime
  deriving

文章和本地display.hamlet

<h1>#{titleA}
<article>#{contexA}

1 个答案:

答案 0 :(得分:1)

所以我将上下文从Html更改为Text。

Article
  title Text
  context Text
  time UTCTime
  deriving

然后在使用值时添加preEscapedText

let contextA = preEscapedText $ articleContext article

现在它正常显示。