Skip to content

Commit

Permalink
Merge pull request #18 from adamwathan/rename-icon-to-svg
Browse files Browse the repository at this point in the history
Rename icon references to SVG
  • Loading branch information
adamwathan authored Jun 2, 2017
2 parents 3ec8f97 + 02bc003 commit 112e8d2
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 94 deletions.
18 changes: 9 additions & 9 deletions config/blade-svg.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@

/*
|--------------------------------------------------------------------------
| Icon Path
| SVG Path
|--------------------------------------------------------------------------
|
| This value is the path to the directory of individual SVG icons. This
| This value is the path to the directory of individual SVG files. This
| path is then resolved internally. Please ensure that this value is
| set relative to the root directory and not the public directory.
|
*/

'icon_path' => 'path/to/icons',
'svg_path' => 'path/to/svgs',

/*
|--------------------------------------------------------------------------
| Spritesheet Path
|--------------------------------------------------------------------------
|
| If you would rather have one spritesheet than a lot of individual svg
| files, you may specify a path to a spritesheet. The icons are then
| If you would rather have one spritesheet than a lot of individual SVG
| files, you may specify a path to a spritesheet. The SVG images are
| extracted from this spritesheet to be rendered out individually.
|
*/
Expand All @@ -46,9 +46,9 @@
| Inline Rendering
|--------------------------------------------------------------------------
|
| This value will determine whether or not the icon must be rendered as
| an SVG element or if it must be referenced on the spritesheet. The
| icon, if this value is false, will be rendered with a 'use' tag.
| This value will determine whether or not the image should be rendered
| as an SVG element or if it must be referenced on the spritesheet. The
| SVG, if this value is false, will be rendered with a 'use' tag.
|
| Default: false
|
Expand All @@ -61,7 +61,7 @@
| Optional Class
|--------------------------------------------------------------------------
|
| If you would like to have CSS classes applied to your icons, you must
| If you would like to have CSS classes applied to your SVGs, you must
| specify them here. Much like how you would define multiple classes
| in an HTML attribute, you may separate each class using a space.
|
Expand Down
44 changes: 22 additions & 22 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Blade SVG

Easily use SVG icons in your Blade templates, either as inline SVG or using SVG sprites.
Easily use SVG images in your Blade templates, either as inline SVG or using SVG sprites.

## Installation

Expand Down Expand Up @@ -34,7 +34,7 @@ Publish the Blade SVG config file:
php artisan vendor:publish --provider="BladeSvg\BladeSvgServiceProvider"
```

If you want to use the sprite sheet instead of rendering every icon inline, make sure you render the hidden sprite sheet somewhere at the end of any layouts that are going to use icons using the `svg_spritesheet()` helper:
If you want to use the sprite sheet instead of rendering every image inline, make sure you render the hidden sprite sheet somewhere at the end of any layouts that are going to use SVGs using the `svg_spritesheet()` helper:

```
<!-- layout.blade.php -->
Expand All @@ -52,42 +52,42 @@ If you want to use the sprite sheet instead of rendering every icon inline, make

### Configuration

Inside `config/blade-svg.php` specify the location of your spritesheet and the path to your individual icons:
Inside `config/blade-svg.php` specify the location of your spritesheet and the path to your individual SVG images:

```php
<?php

return [
'spritesheet_path' => 'resources/assets/svg/sprite.svg',
'icon_path' => 'resources/assets/svg/icons',
'svg_path' => 'resources/assets/svg/icons',
// ...
];
```

> These paths are resolved internally using the `base_path()` helper, so specify them relative to the root of your project.
You can also specify whether you'd like icons to be rendered inline by default, or to reference the icon from the sprite sheet:
You can also specify whether you'd like SVGs to be rendered inline by default, or to reference the SVG from the sprite sheet:

```php
<?php

return [
// ...
'inline' => true, // Render the full icon SVG inline by default
'inline' => true, // Render the full SVG inline by default
// or...
'inline' => false, // Reference the sprite sheet and render the icon with a `use` tag
'inline' => false, // Reference the sprite sheet and render the image with a `use` tag
// ...
];
```

You can specify any default CSS classes you'd like to be applied to your icons using the `class` option:
You can specify any default CSS classes you'd like to be applied to your SVG images using the `class` option:

```php
<?php

return [
// ...
'class' => 'icon', // Add the `icon` class to every SVG icon when rendered
'class' => 'icon', // Add the `icon` class to every SVG when rendered
// ...
];
```
Expand All @@ -104,7 +104,7 @@ return [
];
```

If the ID attributes of the icons in your spritesheet have a prefix, you can configure that using the `sprite_prefix` option:
If the ID attributes of the SVGs in your spritesheet have a prefix, you can configure that using the `sprite_prefix` option:

```php
<?php
Expand All @@ -118,11 +118,11 @@ return [

## Basic Usage

To insert an icon in your template, simply use the `@icon` Blade directive, passing the name of the icon and optionally any additional classes:
To insert an SVG in your template, simply use the `@svg` Blade directive, passing the name of the SVG and optionally any additional classes:

```html
<a href="/settings">
@icon('cog', 'icon-lg') Settings
@svg('cog', 'icon-lg') Settings
</a>

<!-- Renders.. -->
Expand All @@ -138,7 +138,7 @@ To add additional attributes to the rendered SVG tag, pass an associative array

```html
<a href="/settings">
@icon('cog', 'icon-lg', ['alt' => 'Gear icon']) Settings
@svg('cog', 'icon-lg', ['alt' => 'Gear icon']) Settings
</a>

<!-- Renders.. -->
Expand All @@ -154,7 +154,7 @@ If you have attributes to declare but no additional class, you can pass an assoc

```html
<a href="/settings">
@icon('cog', ['alt' => 'Gear icon']) Settings
@svg('cog', ['alt' => 'Gear icon']) Settings
</a>

<!-- Renders.. -->
Expand All @@ -170,7 +170,7 @@ If you'd like to _override_ the default class name, specify a class in the attri

```html
<a href="/settings">
@icon('cog', ['class' => 'overridden']) Settings
@svg('cog', ['class' => 'overridden']) Settings
</a>

<!-- Renders.. -->
Expand All @@ -186,7 +186,7 @@ If you'd like to add an attribute that needs no value, just specify it without a

```html
<a href="/settings">
@icon('cog', ['data-foo']) Settings
@svg('cog', ['data-foo']) Settings
</a>

<!-- Renders.. -->
Expand All @@ -198,11 +198,11 @@ If you'd like to add an attribute that needs no value, just specify it without a
</a>
```

If you'd like, you can use the `svg_icon` helper directly to expose a fluent syntax for setting icon attributes:
If you'd like, you can use the `svg_image` helper directly to expose a fluent syntax for setting SVG attributes:

```html
<a href="/settings">
{{ svg_icon('cog')->alt('Alt text')->dataFoo('bar')->dataBaz() }} Settings
{{ svg_image('cog')->alt('Alt text')->dataFoo('bar')->dataBaz() }} Settings
</a>

<!-- Renders.. -->
Expand All @@ -214,11 +214,11 @@ If you'd like, you can use the `svg_icon` helper directly to expose a fluent syn
</a>
```

You can force an icon to reference the sprite sheet even if you are rendering inline by default by using the fluent syntax and chaining the `sprite` method:
You can force an SVG to reference the sprite sheet even if you are rendering inline by default by using the fluent syntax and chaining the `sprite` method:

```html
<a href="/settings">
{{ svg_icon('cog', 'icon-lg')->sprite() }} Settings
{{ svg_image('cog', 'icon-lg')->sprite() }} Settings
</a>

<!-- Renders.. -->
Expand All @@ -230,11 +230,11 @@ You can force an icon to reference the sprite sheet even if you are rendering in
</a>
```

Similarly, you can force an icon to render inline even if you are using sprites by default by chaining the `inline` method:
Similarly, you can force an SVG to render inline even if you are using sprites by default by chaining the `inline` method:

```html
<a href="/settings">
{{ svg_icon('cog', 'icon-lg')->inline() }} Settings
{{ svg_image('cog', 'icon-lg')->inline() }} Settings
</a>

<!-- Renders.. -->
Expand Down
18 changes: 10 additions & 8 deletions src/BladeSvgServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

namespace BladeSvg;

use BladeSvg\IconFactory;
use BladeSvg\SvgFactory;
use Illuminate\Support\Collection;
use Illuminate\Support\ServiceProvider;

class BladeSvgServiceProvider extends ServiceProvider
{
public function boot()
{
app(IconFactory::class)->registerBladeTag();
app(SvgFactory::class)->registerBladeTag();

$this->publishes([
__DIR__.'/../config/blade-svg.php' => config_path('blade-svg.php'),
Expand All @@ -18,12 +19,13 @@ public function boot()

public function register()
{
$this->app->singleton(IconFactory::class, function () {
$config = array_merge(config('blade-svg', []), [
'spritesheet_path' => config('blade-svg.spritesheet_path') ? base_path(config('blade-svg.spritesheet_path')) : null,
'icon_path' => config('blade-svg.icon_path') ? base_path(config('blade-svg.icon_path')) : null,
]);
return new IconFactory($config);
$this->app->singleton(SvgFactory::class, function () {
$config = Collection::make(config('blade-svg', []))->merge([
'spritesheet_path' => config('blade-svg.spritesheet_path'),
'svg_path' => config('blade-svg.svg_path', config('blade-svg.icon_path')),
])->map('base_path')->all();

return new SvgFactory($config);
});
}
}
12 changes: 6 additions & 6 deletions src/Icon.php → src/Svg.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
use Illuminate\Support\HtmlString;
use Illuminate\Contracts\Support\Htmlable;

class Icon implements Htmlable
class Svg implements Htmlable
{
private $icon;
private $imageName;
private $renderMode;
private $factory;
private $attrs = [];

public function __construct($icon, $renderMode, $factory, $attrs = [])
public function __construct($imageName, $renderMode, $factory, $attrs = [])
{
$this->icon = $icon;
$this->imageName = $imageName;
$this->renderMode = $renderMode;
$this->factory = $factory;
$this->attrs = $attrs;
Expand Down Expand Up @@ -56,7 +56,7 @@ public function renderInline()
return str_replace(
'<svg',
sprintf('<svg%s', $this->renderAttributes()),
$this->factory->getSvg($this->icon)
$this->factory->getSvg($this->imageName)
);
}

Expand All @@ -65,7 +65,7 @@ public function renderFromSprite()
return vsprintf('<svg%s><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="%s#%s"></use></svg>', [
$this->renderAttributes(),
$this->factory->spritesheetUrl(),
$this->factory->spriteId($this->icon)
$this->factory->spriteId($this->imageName)
]);
}

Expand Down
24 changes: 14 additions & 10 deletions src/IconFactory.php → src/SvgFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\Blade;

class IconFactory
class SvgFactory
{
private $files;
private $svgCache;
Expand All @@ -28,14 +28,18 @@ public function __construct($config = [], $filesystem = null)
public function registerBladeTag()
{
Blade::directive('icon', function ($expression) {
return "<?php echo e(svg_icon($expression)); ?>";
return "<?php echo e(svg_image($expression)); ?>";
});

Blade::directive('svg', function ($expression) {
return "<?php echo e(svg_image($expression)); ?>";
});
}

private function iconPath()
private function svgPath()
{
return $this->config->get('icon_path', function () {
throw new Exception('No icon_path set!');
return $this->config->get('svg_path', function () {
throw new Exception('No svg_path set!');
});
}

Expand All @@ -61,7 +65,7 @@ public function spritesheet()
);
}

public function icon($name, $class = '', $attrs = [])
public function svg($name, $class = '', $attrs = [])
{
if (is_array($class)) {
$attrs = $class;
Expand All @@ -72,12 +76,12 @@ public function icon($name, $class = '', $attrs = [])
'class' => $this->buildClass($class),
], $attrs);

return new Icon($name, $this->renderMode(), $this, $attrs);
return new Svg($name, $this->renderMode(), $this, $attrs);
}

public function spriteId($icon)
public function spriteId($svgName)
{
return "{$this->spritePrefix()}{$icon}";
return "{$this->spritePrefix()}{$svgName}";
}

private function spritePrefix()
Expand All @@ -98,7 +102,7 @@ private function buildClass($class)
public function getSvg($name)
{
return $this->svgCache->get($name, function () use ($name) {
return $this->svgCache[$name] = trim($this->files->get(sprintf('%s/%s.svg', rtrim($this->iconPath()), str_replace('.', '/', $name))));
return $this->svgCache[$name] = trim($this->files->get(sprintf('%s/%s.svg', rtrim($this->svgPath()), str_replace('.', '/', $name))));
});
}
}
16 changes: 13 additions & 3 deletions src/helpers.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
<?php

use BladeSvg\IconFactory;
use BladeSvg\SvgFactory;

if (! function_exists('svg_spritesheet')) {
function svg_spritesheet()
{
return app(IconFactory::class)->spritesheet();
return app(SvgFactory::class)->spritesheet();
}
}

if (! function_exists('svg_image')) {
function svg_image($icon, $class = '', $attrs = [])
{
return app(SvgFactory::class)->svg($icon, $class, $attrs);
}
}

if (! function_exists('svg_icon')) {
/**
* @deprecated Use `svg_image`
*/
function svg_icon($icon, $class = '', $attrs = [])
{
return app(IconFactory::class)->icon($icon, $class, $attrs);
return app(SvgFactory::class)->svg($icon, $class, $attrs);
}
}
Loading

0 comments on commit 112e8d2

Please sign in to comment.