发布于 2015-09-26 03:21:07 | 120 次阅读 | 评论: 0 | 来源: 网络整理

这篇文章教你如何创建你的博客贴子的一个列表,每一个条目包含标题、链接以及贴子内容。

目录结构

给定一个这样的目录结构:

/public
  /posts
    _data.json
    my-first-post.md
    my-second-post.md
  /index.jade    <-- or index.ejs

贴子数据

添加一个这样的 /public/posts/_data.json

{
  "my-second-post": {
    "title": "My second post"
  },
  "my-first-post": {
    "title": "My first post"
  }
}

使用 Jade 进行列表

你可以在 Jade 中这样遍历你的贴子:

for post, slug in public.posts._data
  h2: a(href ="/posts/#{ slug }")= post.title
  != partial("posts/" + slug)

使用 EJS 进行列表

或者在 EJS 中:

<% for(var slug in public.posts._data){ %>
  <h2><a href ="/posts/<%= slug %>"><%= public.posts._data[slug].title %></a></h2>
  <%- partial("posts/" + slug) %>
<% }; %>

工作原理

我们正在使用 for 迭代器来遍历 /public/posts/_data.json 中的数据。可以通过 public.posts._data 获取到贴子数据对象。

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务