svg-sprite / svg-sprite

SVG sprites & stacks galore — A low-level Node.js module that takes a bunch of SVG files, optimizes them and bakes them into SVG sprites of several types along with suitable stylesheet resources (e.g. CSS, Sass, LESS, Stylus, etc.)

Home Page:https://github.com/svg-sprite/svg-sprite

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Customize {{ name }} in sprite.html preview (example) sheet

macfire opened this issue · comments

My setup:

  • Each SVG file name is prepended with "icon__" (for legacy reasons)
  • Using grunt-svg-sprite config file
  • Specifying mode.symbol{} configuration
  • All renders correctly.

Objective:

In the sprite sheet, using a modified version of the template "sprite.html", I want to remove the prefix "icon__" from the rendered {{name}} variable.

I've created a function in the "variables" object, which will return a string value.

Is it possible to pass the symbol "name" or "id" value to the custom function, modify, and return value?

Example:

// This does not work, but shows idea
...
"variables": {
    "shortenName": function(_name) {
        return _name.replace("icon__", "");
   }
}
<!-- in sprite.html -->
{{#shapes}}
                <li title="{{name}}">
                    <div class="icon-box">
                        <svg class="{{#selector.dimensions}}{{#last}}{{#classname}}{{raw}}{{/classname}}{{/last}}{{/selector.dimensions}}">
                            <use xlink:href="#{{name}}"></use>
                        </svg>
                    </div>
                    <div>{{{shortenName name }}}</div>
                    <!-- <h2>{{name}}</h2>  -->
                </li>
{{/shapes}}

I'm new to both svg-sprite and mustache templates.

Is my objective achievable?
Is my syntax in mustache template correct?

Thanks.

Answering my own question:

Solution:

...
        "variables": {
            "shortenName": function () {
                return function (val, render) {
                    let new_val = render(val);
                    return new_val.replace("icon__", "");
                }
            },
        }
<!-- in sprite.html -->
<div>{{#shortenName}}{{name}}{{/shortenName}}</div>

My mistake was in trying to pass the value to the top-level function for 'shortenName()', instead of using the 'render' function.