1
votes

I'm trying to use CKEditor in my Yesod application. Data from CKEditor is returned to the server via Textarea, I then store it as Html in database. My problem is I do know know how to display the Html algebraic data type once I retrieve it from database in the handler. I've been reading this tutorial, but it will only display the Html as a big long string, not as markup.

Note: titleA and contextA are the variable that I want to display in article-local-display. contextA is the Html algebraic data type

PS: Do I need to transform Html to hamlet in order to render?

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

models file:

Article
  title Text
  context Html
  time UTCTime
  deriving

article-local-display.hamlet

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

1 Answers

1
votes

So I changed the context from Html to Text.

Article
  title Text
  context Text
  time UTCTime
  deriving

Then added preEscapedText when using the value.

let contextA = preEscapedText $ articleContext article

Now it's displaying properly.