Templates 如何使用hamlet打印逗号分隔的列表?

Templates 如何使用hamlet打印逗号分隔的列表?,templates,haskell,yesod,hamlet,Templates,Haskell,Yesod,Hamlet,使用yesod附带的hamlet模板语言,打印逗号分隔列表的最佳方式是什么 例如,假设此代码只打印一个又一个条目,如何在元素之间插入逗号?或者在最后一个条目之前添加“and”: The values in the list are $ forall entry <- list #{entry} and that is it. 列表中的值为 $forall entry我不认为有任何内置的类似功能。幸运的是,在Hamlet中使用助手函数很容易。例如,如果项目是纯字符串,则可以使用Da

使用yesod附带的hamlet模板语言,打印逗号分隔列表的最佳方式是什么

例如,假设此代码只打印一个又一个条目,如何在元素之间插入逗号?或者在最后一个条目之前添加“and”:

The values in the list are
$ forall entry <- list
    #{entry}
and that is it.
列表中的值为

$forall entry我不认为有任何内置的类似功能。幸运的是,在Hamlet中使用助手函数很容易。例如,如果项目是纯字符串,则可以使用
Data.List.interlate
在它们之间添加逗号

The values in the list are 
#{intercalate ", " list} 
and that is it.
如果你想做更有趣的事情,你可以写函数来处理哈姆雷特的值。例如,这里有一个函数,它在列表中的Hamlet值之间添加逗号和“and”

commaify [x] = x
commaify [x, y] = [hamlet|^{x} and ^{y}|]
commaify (x:xs) = [hamlet|^{x}, ^{commaify xs}|]
这使用
^{…}
语法将一个Hamlet值插入到另一个值中。现在,我们可以用它来写一个逗号分隔的带下划线单词列表

The values in the list are 
^{commaify (map underline list)} 
and that is it.
这里,
underline
只是一个小的辅助函数,用于生成比纯文本更有趣的内容

underline word = [hamlet|<u>#{word}|]
underline word=[hamlet |#{word}}]
渲染时,将产生以下结果

The values in the list are <u>foo</u>, <u>bar</u> and <u>baz</u> and that is it.
列表中的值是foo、bar和baz,就是这样。

谢谢,我想这是一个很好的解决方案集合,尽管没有一个像Template Haskell提供的那样令人满意。也许我应该向hamlet提交一个单独的
循环
变量的补丁。