Yet another Nunjucks renderer for Hexo.
- Nunjucks 3
- Predefined filters
- Full Nunjucks renderer customization via Hexo Filter API
npm install hexo-renderer-njcksYou can customize the Nunjucks renderer via the 2 following ways.
All configs are passed directly to Nunjucks#configure(), available options can be found here.
The following list shows the descending precedence of config files. (The first one presented is used.)
- Key
nunjucksin<hexo_root>/_config.yml - Key
theme_config.nunjucksin<hexo_root>/_config.yml - Key
nunjucksin<hexo_root>/themes/<theme_name>/_config.yml - Inline default config
You can extend the before_render:nunjucks filter whose first argument is an Nunjucks Environment object, which is used later to render templates.
Note that the filter should be synchronized.
For example, you can use the following code in your Hexo script to add a new Nunjucks filter named split to split strings into arrays of strings.
hexo.extend.filter.register('before_render:nunjucks', function SplitFilter (env) {
env.addFilter('split', (str, del) => str.split(del))
})In the template, use the Nunjucks filter as usual.
{% for i in "1 2 3 4 5"|split(' ') %}#{{ i }}{% endfor %}
Output should be
#1#2#3#4#5
Template
{{ "test" | typeof }}
Output
string
Hexo script
hexo.extend.filter.register('before_render:nunjucks', function GlobalStyleObj (env) {
env.addGlobal('DEFAULT_STYLE', {
someAttr: 'a',
other: 'b'
})
})Template
<div {{ DEFAULT_STYLE | xmlattr }}></div>Output
<div some-attr="a" other="b"></div>