I wanted to be able to allow a CMS user to add an SVG icon to a sites' SVG Sprite.

An SVG sprite is the new way of doing image sprites, like the old way of doing CSS image sprites - where you would put all your icons on one image files then use CSS to position that image to show the icon you needed, this is essentially the same thing, but instead it's a list of SVG icons that can be used throughout a site.

See this article on CSS Tricks about creating an 'Icon System with SVG Sprites' - https://css-tricks.com/svg-sprites-use-better-icon-fonts/

Usually, SVG sprites are created by a developer/designer when building a website and then never revisited unless the site has a redesign. The creation of a sprite is likely to be an automated process using a task runner like Grunt or Gulp JS.

But this process does not apply to a site once it's gone live and is managed by a content management system (CMS).

A similar technique inspired me to create this. See Clive Walker's post - http://www.cvwdesign.com/blog/using-perch-layouts-and-managing-svg-assets-with-perch

Let's get into the code

To start, you need to create a PHP file which will list all the SVG icons. This PHP file has to be included on all pages - but they will be hidden. This file is not in the root of the site and is not indexed by search engines.

Once you have that setup, you can include the ability to add SVG icons to your templates, and all the user needs to do is insert the ID of that SVG.

Create a PHP file inside the /perch /includes/ - you don't have to create the includes folder, but I like to keep these types of files separate.

I called the page svgs.php but you can name it anything - icons.php may have made more sense.

In this PHP file, I have a perch_content tag called svgs - again this could be called anything. Make sure you include the runtime.php as per usual and then go to that file on your website: websiteaddress.uk/cms/svgs.php - you can now edit that page via Perch CMS.

You need to create a new template for adding icons to the svgs.php file. Create the template in /templates/content/ and you can call it anything you want; I called mine SVG_icons.html and use the following code:

perch:before </perch:after>

Now log in to your Perch CMS and go to Pages > svgs, click on the Icons region and select the new template - SVG Icons - make sure you check 'Allow multiple items' so that you can add multiple icons.

You need to include this code on all the pages that you want to be able to use the SVG icons on - I would add it to you header layout - make sure it's placed just after the tag.

'/cms/includes/svgs.php', 'template'=>'SVG_icons.html' )); ?>

Let's go back to the SVGs page - Pages > svgs and let's add an icon.

Adding an SVG icon

The SVG_icons.html template gives you the following fields:

  • Title - if you are not hiding this icon for visually impaired users, you should give it a title
  • ID - this has to be a unique name as it will be used to refer to the icon
  • Width and Height
  • ViewBox and SVG Paths.

Fill all of these in - you get this information by opening the SVG using your IDE - like Sublime Text.

Save this.

Once saved, that icon should now be in the svgs.php page - but it won't be visible because we are hiding the SVG container.

This is what the output will look like inside the tag in the svgs.php file:

html5 Icon

Include the following code on any template where you want the user to be able to add an SVG icon from the sprite.

By adding the above code in a template, in Perch, you can use an SVG icon from the svgs.php file.

Adding an SVG icon to your content

You should now be able to add your own SVG icons to your SVG sprite via Perch CMS.

This solution is what I am using on my own website and will be using in all future website that I create that use Perch CMS.

What do you think of this solution? Can you see any way this can be improved? I'd love to get some feedback - this is the first time I have shared any code that I have used in Perch.

Posted on , by in development