如何在帖子中使用nunjuck`include`?

时间:2017-11-21 18:02:26

标签: nunjucks hexo

我正在玩hexo,我正在测试nunjuck语法,它适用于一个小循环。但是我无法找到include a file的方法,我的文件位置可能出错(目前位于.md的{​​{1}}旁边)。

环境信息

节点版本(/source/_posts):

node -v

您的网站node --version; npm --version v8.9.1 5.5.1 (可选):

_config.yml

Hexo和插件版本(# 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> ):

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

button6.md 中我添加了

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

错误

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

问题

我在_hexo-demo / source / posts / button6.md

中发表了这篇文章
Unhandled rejection Template render error: (unknown path)
  Error: template not found: include/colors.html

问题

我应该在哪里放置--- title: button6 myitems: - one - two --- {% for item in myitems %} <li> {{ item }}</li> {% endfor %} <hr> {% include "colors.html" %} 文件才能解决问题

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

2 个答案:

答案 0 :(得分:1)

在hexo中没有本机支持。标签的灵感来自Octopress,据我所知,octop中的render_partial标签不存在于hexo中。

好消息是有一个hexo插件:

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

安装完成后,您只需执行

即可
{% include colors.html %}

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

答案 1 :(得分:1)

我遇到了同样的问题。 似乎包含十六进制与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' %}

它有效。