繁体   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