Coder Social home page Coder Social logo

gilesbowkett / eleventy-plugin-svg-contents Goto Github PK

View Code? Open in Web Editor NEW

This project forked from brob/eleventy-plugin-svg-contents

0.0 2.0 0.0 175 KB

An Eleventy plugin to grab the contents of an SVG file to allow for embedding in your template with all the power of SVG.

JavaScript 81.84% HTML 18.16%

eleventy-plugin-svg-contents's Introduction

eleventy-plugin-svg-contents

An Eleventy universal plugin to grab the contents of an SVG file to allow for embedding in your template with all the power of SVG.

Installation

Available on npm.

npm install eleventy-plugin-svg-contents --save

Open up your Eleventy config file (probably .eleventy.js) and add the plugin:

const svgContents = require("eleventy-plugin-svg-contents");
module.exports = function(eleventyConfig) {
  eleventyConfig.addPlugin(svgContents);
};

Usage

Base usage

In your (Nunjucks, Liquid or Handlebars) templates, use the following syntax to grab the contents from any SVG in your project path:

// nunjucks/liquid
{{ 'path/to/file.svg' | svgContents }}

// handlebars
{{{svgContents 'path/to/file.svg' }}}

Quick note: You may need to pass another filter after to have this render as html. In Nunjucks, you'll add | safe to the end.

Adding a class to your SVG

You can append the svg class with the class option.

// Nunjucks
{{ 'path/to/file.svg' | svgContents("extra-class-one extra-class-two") }}

// Liquid
{{ 'path/to/file.svg' | svgContents: "extra-class-one extra-class-two" }}

// Handlebars
{{{svgContents 'path/to/file.svg' "extra-class-one extra-class-two" }}}

Using other SVG elements

If you want to use an element selector other than SVG (or want to select a specific piece of an SVG) you can use an optional second argument to provide a selector for the element in your SVG file. Any selector strings will work (classes, ids, attr, element).

Due to limitations of Liquid and Handlebars, you'll need to have a placeholder string for the optional class name argument.

// Nunjucks
{{ 'path/to/file.svg' | svgContents("", "symbol") }}
{{ 'path/to/file.svg' | svgContents("add-class", "#byId") }}

// Liquid
{{ 'path/to/file.svg' | svgContents: "", "symbol" }}
{{ 'path/to/file.svg' | svgContents: "add-class", "#byId" }}

// Handlebars
{{{svgContents 'path/to/file.svg' "" "symbol" }}}
{{{svgContents 'path/to/file.svg' "add-class" "#byId" }}}

Filters

  • svgContents: Grabs the xml from an SVG and passes it to your template. Voila! Instant embedded SVG from a file!
  • Future Plan stripTitle: A filter to strip the <title> that often comes with SVGs and can cause havoc with SEO.

eleventy-plugin-svg-contents's People

Contributors

brob avatar dependabot[bot] avatar courdek avatar dawaltconley avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.