Search code examples
nunjuckshexo

How do I use the HexoJS css helper from a nunjucks template?


I get the error:

Unhandled rejection Template render error: (unknown path) [Line 9, Column 6]
  unknown block tag: css
    at Object.exports.withPrettyErrors (/Users/me/hexo-site/node_modules/hexo-renderer-nunjucks/node_modules/nunjucks/src/lib.js:35:17)

My themes/theme-name/layout/layout.nunjucks file has this code:

<title>Site title</title>
{% css "css/style.css" %}

Solution

  • Nunjucks does not have a css tag, and it doesn't look like hexo-renderer-nunjucks implements one either.

    You can implement it yourself, but it looks easier to just use a filter:

    env.addFilter('css', function(str) {
        return '<link rel="stylesheet" href="' + str + '"></link"'
    })
    

    and use {{"css/style.css"|css}} instead.