【问题标题】:Hakyll - Using makeItem with data to create a list in HTMLHakyll - 使用带有数据的 makeItem 在 HTML 中创建列表
【发布时间】:2015-08-14 06:59:00
【问题描述】:

我有一个类别列表。每个类别本身都有一个子类别列表:[(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) &gt;&gt; loadAndApplyTemplate ".." contextWithCategories,然后以某种方式在实际页面生成中引用生成的数据)?

【问题讨论】:

    标签: haskell hakyll


    【解决方案1】:

    我自己解决了这个问题。 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-14
      • 2020-12-03
      • 2021-08-02
      • 1970-01-01
      • 1970-01-01
      • 2020-06-10
      • 2018-05-30
      相关资源
      最近更新 更多