Skip to content
Snippets Groups Projects

[FEATURE] Extend mustache template context using global options.context variable…

Merged Thomas requested to merge thomas/gulp-inline-svg:context-and-file-interceptor into master

[FEATURE] Extend mustache template context using global options.context variable or alter context of SVG data

Merge request reports

Loading
Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
  • Author Contributor

    Hi,

    Great plugin!

    We've made some adjustments. With these changes you can add context on the root template and/or modify the context per SVG, allowing this template to be used:

    {{message}}
    
    {{#svgs}}
      ${{prefix}}-{{{variableName}}}: "{{{inline}}}";
    {{/svgs}}
    // gulpfile.js
    gulp.task('inline-svg', function() {
      return gulp.src('assets/icons/**/*.svg')
        .pipe(inlineSvg({
          filename: 'icons.scss',
          template: 'assets/icons/template.mustache',
          context: {
            message: '// GENERATED BY gulpfile.js'
          },
          interceptor: function (svgData, file) {
            return Object.assign(svgData, { variableName: path.basename(file.path, '.svg').toLowerCase(), prefix: 'dso-icon' });
          }
        }))
        .pipe(gulp.dest('src/styles/icons'));
    });

    Output:

    // GENERATED BY gulpfile.js
    
    $icon-error_32x32: "data:image/svg+xml;charset=utf8,%3C?xml version='1.0' [..] %3C/g%3E %3C/svg%3E";
    $icon-information_32x32: "data:image/svg+xml;charset=utf8,%3C?xml version='1.0' [..] %3C/g%3E %3C/svg%3E";
    $icon-succes_32x32: "data:image/svg+xml;charset=utf8,%3C?xml version='1.0' [..] %3C/g%3E %3C/svg%3E";
    $icon-warning_32x32: "data:image/svg+xml;charset=utf8,%3C?xml version='1.0' [..] %3C/g%3E %3C/svg%3E";

    This is a simple example, but basically we want to control the name variable: We want to generate the variable name using the SVG's filename AND path

    Edited by Thomas
  • mentioned in commit 095c5dbf

  • Hi @thomas,

    thanks so much for contributing! This looks like a great addition. I just merged your changes and released it to NPM as version 1.2.0 :-)

  • Author Contributor

    Sweet, thanks! Keep up the good work :-)

Please register or sign in to reply
Loading