From 90e87775719b0a47d71d0f135528c0465623134e Mon Sep 17 00:00:00 2001 From: Solovyev Aleksey Date: Fri, 23 Apr 2021 18:53:32 +0300 Subject: [PATCH] Add global nunjucks function - icon --- config/modules/njk-loader/functions/icon.js | 13 +++++++++++++ config/modules/njk-loader/index.js | 4 ++++ 2 files changed, 17 insertions(+) create mode 100644 config/modules/njk-loader/functions/icon.js diff --git a/config/modules/njk-loader/functions/icon.js b/config/modules/njk-loader/functions/icon.js new file mode 100644 index 0000000..217ee43 --- /dev/null +++ b/config/modules/njk-loader/functions/icon.js @@ -0,0 +1,13 @@ +/** + * Simplifies work with SVG sprite. + * + * @param {string} name - SVG icon name + * @returns {string} + * + * @example + * 1. Import SVG ivon (app.js) + * import '@/icons/heart.svg' + * 2. Use it in nunjucks template (template.njk) + *
{{ icon('heart') | safe }}
+ */ +module.exports = name => `` diff --git a/config/modules/njk-loader/index.js b/config/modules/njk-loader/index.js index 4a08b3b..2527bac 100644 --- a/config/modules/njk-loader/index.js +++ b/config/modules/njk-loader/index.js @@ -1,6 +1,7 @@ const path = require('path') const { Environment, FileSystemLoader } = require('nunjucks') const schema = require('./schema') +const iconFn = require('./functions/icon') /** @@ -30,6 +31,9 @@ module.exports = function(source) { this.addDependency(source.path) }) + // Add a global value that will be available to all templates. + env.addGlobal('icon', iconFn) + env.renderString(source, context, (error, HTMLString) => { callback(error ? error : null, HTMLString) })