如何在Metalsmith中使用下划线的部分?

时间:2017-04-17 10:26:31

标签: underscore.js partials underscore.js-templating metalsmith

我正在为我的Metalsmith网站使用Underscore的模板引擎,我在访问footer部分时遇到了一些麻烦。我收到一条错误消息:

ReferenceError: footer is not defined

我该怎么称呼它?我做错了什么?

谢谢。

这是我的Metalsmith构建文件的模板部分:

...
.use(layouts({
  engine: 'underscore',
  directory: 'templates',
  partials: 'templates/partials'
}))

这是我的文件夹结构:

posts/
src/
templates/
- base.tpl.html
- partials/
-- footer.tpl.html

这是一个帖子示例:

---
title: My First Post
date: 2012-08-20
layout: base.tpl.html
---

# This is my title

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur sed magna vel eros malesuada fringilla.

这是我的html页面:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title><%- sitename %></title>
  </head>
  <body>
    <h1>Hello, world!</h1>

    <%= contents %>

    <% footer %>    <---- THIS IS UNDEFINED
  </body>
</html>

1 个答案:

答案 0 :(得分:1)

我猜您需要在构建文件中使用 partialExtension 选项:

...
.use(layouts({
  engine: 'underscore',
  directory: 'templates',
  partials: 'templates/partials',
  partialExtension: '.tpl.html'
}))

https://www.npmjs.com/package/metalsmith-layouts#partialextension

...因为当插件处理<% footer %>时,它实际上只搜索名为footer的文件,而不是footer.tpl.html

(未经测试)