【问题标题】:How to use nunjuck `include` in post?如何在帖子中使用 nunjucks `include`?
【发布时间】:2017-11-21 18:02:26
【问题描述】:

我正在玩 hexo,我正在测试适用于小循环的 nunjuck 语法。但是我找不到include a file 的方法,我的文件位置可能有误(目前在/source/_posts 中的.md 旁边)。

环境信息

节点版本(node -v):

node --version; npm --version
v8.9.1
5.5.1

您的网站_config.yml(可选):

# Hexo Configuration
## Docs: https://hexo.io/docs/configuration.html
## Source: https://github.com/hexojs/hexo/

# Site
title: Hexo
subtitle:
description:
author: John Doe
language:
timezone:

# URL
## If your site is put in a subdirectory, set url as 'http://yoursite.com/child' and root as '/child/'
url: http://yoursite.com
root: /
permalink: :year/:month/:day/:title/
permalink_defaults:

# Directory
source_dir: source
public_dir: public
tag_dir: tags
archive_dir: archives
category_dir: categories
code_dir: downloads/code
i18n_dir: :lang
skip_render:

# Writing
new_post_name: :title.md # File name of new posts
default_layout: post
titlecase: false # Transform title into titlecase
external_link: true # Open external links in new tab
filename_case: 0
render_drafts: false
post_asset_folder: false
relative_link: false
future: true
highlight:
  enable: true
  line_number: true
  auto_detect: false
  tab_replace:

# Home page setting
# path: Root path for your blogs index page. (default = '')
# per_page: Posts displayed per page. (0 = disable pagination)
# order_by: Posts order. (Order by date descending by default)
index_generator:
  path: ''
  per_page: 10
  order_by: -date

# Category & Tag
default_category: uncategorized
category_map:
tag_map:

# Date / Time format
## Hexo uses Moment.js to parse and display date
## You can customize the date format as defined in
## http://momentjs.com/docs/#/displaying/format/
date_format: YYYY-MM-DD
time_format: HH:mm:ss

# Pagination
## Set per_page to 0 to disable pagination
per_page: 10
pagination_dir: page

# Extensions
## Plugins: https://hexo.io/plugins/
## Themes: https://hexo.io/themes/
theme: landscape

# Deployment
## Docs: https://hexo.io/docs/deployment.html
deploy:
  type:
</details>

Hexo 和插件版本(npm ls --depth 0):

"hexo": "^3.2.0",
"hexo-generator-archive": "^0.1.4",
"hexo-generator-category": "^0.1.3",
"hexo-generator-index": "^0.2.0",
"hexo-generator-tag": "^0.2.0",
"hexo-renderer-ejs": "^0.3.0",
"hexo-renderer-stylus": "^0.3.1",
"hexo-renderer-marked": "^0.3.0",
"hexo-server": "^0.2.0"

目录结构

tree ./
./
├── include
│   └── colors.html
└── _posts
    └── button6.md

用法

npm install --save --only=prod hexo-include

button6.md 我添加了

{% include "include/colors.html" %}

错误

Unhandled rejection Template render error: (unknown path)
  Error: template not found: include/colors.html

提问

我在 _hexo-demo/source/posts/button6.md 中有这篇文章

---
title: button6
myitems:
  - one
  - two
---

{% for item in myitems %}
<li> {{ item }}</li>
{% endfor %}

<hr>
{% include "colors.html" %}

问题

我应该把我的colors.html 文件放在哪里才能解决

相关: https://github.com/hexojs/hexo/issues/2866

【问题讨论】:

    标签: nunjucks hexo


    【解决方案1】:

    我遇到了同样的问题。 似乎 hexo-include 与 numjack 冲突。 我改变了 lib/index.js 以下

    hexo.extend.tag.register('include_alt', include, {asyn: true});
    

    但它仍然无法正常工作。 这可能是因为渲染时间。 所以我改变了 lib/index.js 以下

    var fs = require('hexo-fs');
    var nunjucks = require('nunjucks');
    var pathFn = require('path');
    // hexo.extend.tag.register('include_alt', include, { asyn: true });
    hexo.extend.tag.register('include_alt', function (args) {
      var path = pathFn.join(hexo.source_dir, args[0]);  
      return new Promise(function(resolve, reject) {
        nunjucks.render(path, function(err, res) {
          if (err) {
            return reject(err);
          }
          resolve(res);
        });
      });
    }, {async: true});
    

    然后我把它当作

    {% include_alt 'some.html' %}
    

    它有效。

    【讨论】:

      【解决方案2】:

      在 hexo 中没有对它的原生支持。标签的灵感来自 Octopress,据我所知,来自 octopress 的 render_partial 标签在 hexo 中不存在。

      好消息是有一个 hexo 插件:

      https://github.com/PirtleShell/hexo-include

      安装后,你可以简单地做

      {% include colors.html %}
      

      colors.html 将位于您的 source 文件夹的根目录中

      【讨论】:

      • 我仍然遇到同样的错误:Error: template not found: include/colors.html
      • 您是否正确安装了插件?您能否详细说明您的文件的确切存储位置以及您使用的标签是什么?
      • @ÉdouardLopez 运气好吗?我有同样的问题
      猜你喜欢
      • 1970-01-01
      • 2018-05-04
      • 2021-11-10
      • 2021-04-05
      • 2014-03-12
      • 1970-01-01
      • 2017-11-16
      • 1970-01-01
      • 2017-10-27
      相关资源
      最近更新 更多