2013-04-07 70 views
6

我希望修改以下代码,以便与其生成站点上最新的三个帖子的链接,而不是在传统博客中完全复制帖子的正文。我在理解下面发生的事情方面有点困难,以及必要的改变是什么。修改Hakyll示例网站

match "index.html" $ do 
    route idRoute 
    compile $ do 
     let indexCtx = field "posts" $ \_ -> 
          postList $ fmap (take 3) . recentFirst 

     getResourceBody 
      >>= applyAsTemplate indexCtx 
      >>= loadAndApplyTemplate "templates/default.html" postCtx 
      >>= relativizeUrls 

回答

3

这并非完全微不足道。第一步是引入snapshots

正如本教程中所解释的,这可确保您可以在您的索引上包含博文,而无需将模板首先应用于HTML。所以,你会得到这样的:

match "posts/*" $ do 
    route $ setExtension "html" 
    compile $ pandocCompiler 
     >>= loadAndApplyTemplate "templates/post.html" postCtx 
     >>= saveSnapshot "content" 
     >>= loadAndApplyTemplate "templates/default.html" postCtx 
     >>= relativizeUrls 

现在,为了显示索引页面上的帖子,你将能够使用该职位的整个$body$。为了做到这一点,你只需要更新templates/post-item.html成类似:

<div> 
    <a href="$url$"><h2>$title$</h2></a> 
    $body$ 
</div> 
+1

这样做似乎重现除了每篇文章的内容之外的所有标题。 – pgay 2013-07-12 04:01:59

1

我知道这个帖子是有点老,但因为它似乎并不在这里解决的是怎么去了。

首先保存快照通过@jaspervdj描述:用于index.html负载

match "posts/*" $ do 
    route $ setExtension "html" 
    compile $ pandocCompiler 
    >>= loadAndApplyTemplate "templates/post.html" postCtx 
    >>= saveSnapshot "content" 
    >>= loadAndApplyTemplate "templates/default.html" postCtx 
    >>= relativizeUrls 

然后将所有交快照与loadAllSnapshots:施加default模板之前

match "index.html" $ do 
    route idRoute 
    compile $ do 
    posts <- recentFirst =<< loadAllSnapshots "posts/*" "content" 
    let indexCtx = listField "posts" postCtx (return posts) `mappend` 
        defaultContext 

由于拍摄快照,该$for(posts)$内的$body$的值将只是每个帖子模板的内容,而不应用默认模板。