3

I'm playing around with hexo, I'm testing the nunjuck syntax which work for a small loop. However I can't find how to include a file, I might be wrong in the location of my file (currently next to the .md, in /source/_posts).

Environment Info

Node version(node -v):

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

Your site _config.yml (Optional):

# 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 and Plugin version(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"

Directory Structure

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

Usage

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

In button6.md I added

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

Error

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

For question

I've this post in _hexo-demo/source/posts/button6.md

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

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

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

Question

Where am I supposed to put my colors.html file in order to be resolve

related: https://github.com/hexojs/hexo/issues/2866

Édouard Lopez
  • 40,270
  • 28
  • 126
  • 178

2 Answers2

2

I got a same issue. It seems hexo-include conflicting with numjack. I changed lib/index.js following

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

but it still won't work. It may be because of rendering timing. So I changed lib/index.js following

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});

then I use it as

{% include_alt 'some.html' %}

and it works.

Masatoshi
  • 197
  • 1
  • 6
0

There is no native support for that in hexo. Tags are inspired by Octopress and the render_partial tag from octopress is not present in hexo as far as I know.

Good news is there is an hexo plugin for this:

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

Once installed, you can simply do

{% include colors.html %}

where colors.html would be at the root of your source folder

klugjo
  • 19,422
  • 8
  • 57
  • 75