我正在尝试在我的 Yesod 应用程序中使用 CKEditor。来自 CKEditor 的数据通过 Textarea 返回到服务器,然后我将其存储Html
在数据库中。我的问题是,一旦我从处理程序的数据库中检索到 Html 代数数据类型,我就知道如何显示它。我一直在阅读本教程,但它只会将 Html 显示为一个大长字符串,而不是标记。
注意:titleA
andcontextA
是我想在 article-local-display 中显示的变量。
contextA
是Html
代数数据类型
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}