jkphl / grunt-iconizr

SVG + PNG icon kit generator — Grunt plugin wrapping around node-iconizr that creates a CSS icon kit from a bunch of SVG files, serving them as SVG / PNG sprites or embedded data URIs along with suitable CSS / Sass / LESS / Stylus etc. stylesheet resources and a JavaScript loader for easy integration into your HTML documents
http://iconizr.com
MIT License
104 stars 9 forks source link

Image dimensions without additional class #7

Closed thulstrup closed 10 years ago

thulstrup commented 10 years ago

Is there a way to get the image dimensions without having to add an additional class to the markup?

At the moment the css looks like this (with dims set to true):

.icon-star { 
  background-position: 0 0;
  background-repeat: no-repeat;
  background-image: url(icons/icons.svg);
}

.icon-star-dims {
  width: 32px;
  height: 32px;
}

What I would like is this:

.icon-star { 
  background-position: 0 0;
  background-repeat: no-repeat;
  background-image: url(icons/icons.svg);
  width: 32px;
  height: 32px;
}
jkphl commented 10 years ago

Hey Rasmus,

that's definitely possible — by appropriately customizing the rendering templates respectively using custom templates. You didn't mention that you're using Sass or LESS, so I'll assume you're outputting plain CSS, right? Do it like this (untested — I'm on holiday in a camping car and only on my mobile phone right now):

In your Gruntfile.js:

/* ... */
render: {
    css: 'path/to/custom/template.css'
}
/* ... */

In your path/to/custom/template.css (relative to your main output directory):

{{#common}}.{{prefix}} {
    background-repeat: no-repeat;
    background-image: url({{sprite}});
}

{{/common}}{{#svg}}{{#selector}}.{{expression}}{{^last}},
{{/last}}{{/selector}} { {{#sprite}}
    background-position: {{position}};{{/sprite}}{{^common}}
    background-repeat: no-repeat;
    background-image: url({{#sprite}}{{sprite}}{{/sprite}}{{^sprite}}{{#encode}}"{{encoded}}"{{/encode}}{{^encode}}{{path}}{{/encode}}{{/sprite}});{{#dims}}{{#dimensions}}
    width: {{width}}px;
    height: {{height}}px;{{/dimensions}}{{/dims}}{{/common}}
}

{{/svg}}

Does this work?

Cheers from Japan, Joschi

thulstrup commented 10 years ago

Custom render templates is exactly what I need.

I've made a minor adjustment to the Grunt task but otherwise your example worked perfectly.

/* ... */
render: {
  css: {
    template: 'path/to/custom/template.css',
    dest: 'path/to/dest/file.css'
  }
}
/* ... */

Thanks for helping out and enjoy the rest of your holiday :-)

jkphl commented 10 years ago

Cool! Sorry, yeah, I forgot about the distinct definition of the template file — but I see you got it anyway. ;) Happy spriting!