A Twig helper for SVG icons
This module requires Craft CMS 3.0.0-RC1 or later.
To install the module, follow these instructions.
-
Open your terminal and go to your Craft project:
cd /path/to/project
-
Then tell Composer to load the plugin:
composer require gentsagency/craft-inline-icons
-
Add contents of the
config/app.php
file to yourconfig/app.php
(or just copy it there if it does not exist). This ensures that your module will get loaded for each request. The file might look something like this:return [ 'modules' => [ 'inline-icons' => [ 'class' => \gentsagency\inlineicons\InlineIcons::class, ], ], 'bootstrap' => ['inline-icons'], ];
The module is intended to work together with @gentsagency/gulp-registry; and more specifically the icons task.
It provides an easy {{ icon('facebook') }}
Twig function to render icons in your Craft templates.
The examples below assume all front-end assets are built with @gentsagency/gulp-registry to a path within the Craft
@webroot
path.
<ul class="socials">
<li>
<a href="https://facebook.com/gentsagency">
{{ icon('facebook') }}
</a>
</li>
<li>
<a href="https://instagram.com/gentsagency">
{{
icon(
'instagram',
{
class: 'inline-icon',
inline: true,
alt: 'Follow us on Instagram'
}
)
}}
</a>
</li>
</ul>
You can pass these options to the Twig function.
class (default: 'icon')
: what class`s should be set on the SVG elementinline (default: false)
: when set to true, the SVG will be parsed by the PHP and embedded in the codealt (default: icon name)
: use this to override the text read by screen readers; set tofalse
to hide the icon from screen readerspath (default: '/assets/icons/icons.svg')
: where the SVG icon spritesheet lives, gets prepended with@webroot
When using inline: false
(the default), icons will not show up in Internet Explorer unless you include the svg4everybody polyfill.
Inlined icons will work everywhere and give you some more styling options. Be aware that parsing the SVG in PHP might impact server performance. Use sparingly and with care!