Hakyll - 使用makeItem和数据在HTML中创建列表

时间:2015-08-14 06:59:01

标签: haskell hakyll

我有一个类别列表。每个类别本身都有一个子类别列表:[(Category,[SubCategory])]。我想在一个页面中获得以下HTML输出:

<h2>Category 1</h2>

<ul>
  <li>Subcategory 1</li>
  <li>Subcategory 2</li>
</ul>

<h2>Category 2</h2>

<ul>
  <li>Subcategory 1</li>
  <li>Subcategory 2</li>
</ul>

我无法找到一个很好的方法来做到这一点。我是否需要将makeItem应用于列表并执行类似

的操作
categoryList = [("Category 1",["Subcategory 1","Subcategory 2"])]

compile $ do
  makeItem (map fst categoryList)
  >>= loadAndApplyTemplate "templates/categories.html" defaultContext

如何在上下文中添加子类别,以便它们在模板中可用?

也许我需要拆分两个创建步骤(类似mapM $ makeItem (map fst categoryList) >> loadAndApplyTemplate ".." contextWithCategories,然后以某种方式引用实际页面生成中生成的数据)?

1 个答案:

答案 0 :(得分:3)

我自己解决了这个问题。 See this post for a detailed description

这是实际执行此操作的代码:

create ["test.html"] $ do
  route idRoute
  compile $ do
  let ctx =
          listField "parents" 
                    (
                     field "parent" (return . fst . itemBody) <>
                     listFieldWith "children" 
                                   (field "child" (return . itemBody)) 
                                   (sequence . map makeItem . snd . itemBody)
                    ) 
                    (sequence [makeItem ("p1",["c1","c2"]), 
                               makeItem ("p2",["p3","p4"])]) <>
          defaultContext
  makeItem ""
      >>= loadAndApplyTemplate "templates/testing.html" ctx
      >>= loadAndApplyTemplate "templates/default.html" defaultContext
      >>= relativizeUrls