簡體   English   中英

Haskell Snap框架-具有Heist的動態超鏈接

[英]Haskell Snap Framework - Dynamic hyperlinks with Heist

我正在嘗試使用Heist模板系統創建動態鏈接。 問題是鏈接顯示為文本,而不是被解釋為html。 是否有使用Heist創建動態列表的特定方法?

構造鏈接的函數:

renderCategories :: Monad m => Db.Category -> I.Splice m
renderCategories (Db.Category catid catname catdesc) =
  I.runChildrenWithText [ ("categoryId", T.concat $ ["<a    href='http://localhost:8000/thread_home?cateid=", T.pack . show $ catid, "'>", T.pack . show $ catid, "</a>"])
    , ("categoryName", catname)
    , ("categoryDesc", catdesc)]

標記在網頁上顯示為“ http:// localhost:8000 / thread_home?cateid = 1'> 1”文本。 來源顯示如下:

&lt;a href='http://localhost:8000/thread_home?cateid=1'&gt;1&lt;/a&gt;

我認為我需要讓它打印實際的<和>,但是我不確定如何實現。 由於我當前正在運行runChildrenWithText來填充此Heist模板,因此更改為runChildrenWith時需要剪接而不是文本,因此我不希望嘗試這種方法,而不希望將'<'和'>'轉換為'&lt'和'&gt'。 任何幫助表示贊賞!

編輯

我正在嘗試使用以下方法手動創建鏈接:

renderCategories :: Monad m => Db.Category -> I.Splice m
renderCategories (Db.Category catid catname catdesc) =
  I.runChildrenWith [ ("categoryId", return $ X.Element "a"[("href", "http://localhost")] $ X.TextNode (T.pack $ show catid))]

但是我遇到兩個錯誤:

Couldn't match type `X.Node' with `[X.Node]'
Expected type: I.Splice m
  Actual type: heist-0.11.1:Heist.Types.HeistT m m X.Node
In the expression:
  return
  $ X.Element "a" [("href", "http://localhost")]
    $ X.TextNode (T.pack $ show catid)

Couldn't match expected type `[X.Node]' with actual type `X.Node'
In the return type of a call of `X.TextNode'
In the second argument of `($)', namely
  `X.TextNode (T.pack $ show catid)'

目前,我還不太了解這些錯誤,感謝您的幫助。

返回鏈接和普通文本的工作函數:

renderCategories :: Monad m => Db.Category -> I.Splice m
renderCategories (Db.Category catid catname catdesc) =
I.runChildrenWith [( "categoryId", return $ [X.Element "a" [("href", T.concat $     ["http://localhost:8000/thread_home?cateid=", T.pack $ show catid] )] [X.TextNode (T.pack $  show catid)] ] )
, ("categoryName", I.textSplice catname)
, ("categoryDesc",  I.textSplice catdesc)]

您看到的行為恰恰是預期的。 您遇到問題的原因是因為您使用的是runChildrenWithText ,這是專為要返回文本節點的情況而設計的更高級別的函數。 它用於需要頁面上的實際文本時。 您所看到的是實現該目標的正確方法。

接頭是返回節點列表的計算。

type Splice n = HeistT n n [Node]

Node以Haskell類型表示DOM,因此,如果要返回鏈接,則應執行以下操作:

return $ [Element "a" [("href", "http://localhost")] [TextNode (T.pack $ show catid)]]

要使用這種拼接,您需要使用runChildrenWith而不是runChildrenWithText

如果手動創建Node看起來很丑,那么還有一個更方便的選擇。 如果導入模塊Text.Blaze.Renderer.XmlHtml ,則會在其中找到函數,使您可以使用blaze-html語法生成Node樹。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM