How to print comma separated list with tree?

Regarding the template language that comes with Yesod, the best way to print a comma separated list?

eg. suppose this code that just prints one record after another, how do I insert commas between elements? Or perhaps even add an β€œand” to the last entry:

The values in the list are $ forall entry <- list #{entry} and that is it. 

Some template languages, such as the Template Toolkit, provide guidelines for detecting the first or last iteration.

+6
source share
1 answer

I do not think that there is something built in. Fortunately, Hamlet is easy to use helper functions. For example, if your elements are string strings, you can simply use Data.List.intercalate to add commas between them.

 The values in the list are #{intercalate ", " list} and that is it. 

If you want to do more interesting things, you can write functions to work with Hamlet values. For example, here is a function that adds commas and "and" between the values ​​of Hamlet in the list.

 commaify [x] = x commaify [x, y] = [hamlet|^{x} and ^{y}|] commaify (x:xs) = [hamlet|^{x}, ^{commaify xs}|] 

In this case, the syntax ^{...} is used to insert one Hamlet value into another. Now we can use this to write a list of underlined words separated by commas.

 The values in the list are ^{commaify (map underline list)} and that is it. 

Here, underline is just a small helper function to create something more interesting than plain text.

 underline word = [hamlet|<u>#{word}|] 

When rendering this gives the following result.

 The values in the list are <u>foo</u>, <u>bar</u> and <u>baz</u> and that is it. 
+5
source

Source: https://habr.com/ru/post/897935/


All Articles