tomhatzer/blade-svg-origin 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tomhatzer/blade-svg-origin

Composer 安装命令:

composer require tomhatzer/blade-svg-origin

包简介

README 文档

README

Blade SVG

Easily inline SVG images in your Blade templates.

Installation

You can install this package via Composer by running this command in your terminal in the root of your project:

composer require tomhatzer/blade-svg-origin

Getting started

The package's service provider will automatically register itself.

Publish the Blade SVG config file:

php artisan vendor:publish --provider="BladeSvg\BladeSvgServiceProvider"

Configuration

Inside config/blade-svg.php, you can specify any default CSS classes you'd like to be applied to your SVG images using the class option:

<?php

return [
    // ...
    'class' => 'icon', // Add the `icon` class to every SVG when rendered
    // ...
];

You can specify multiple classes by separating them with a space, just like you would in an HTML class attribute:

<?php

return [
    // ...
    'class' => 'icon inline-block',
    // ...
];

Basic usage

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

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

<!-- Renders.. -->
<a href="/settings">
    <svg class="icon icon-lg">
        <path d="..." fill-rule="evenodd"></path>
    </svg>
    Settings
</a>

To add additional attributes to the rendered SVG tag, pass an associative array as the third parameter:

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

<!-- Renders.. -->
<a href="/settings">
    <svg class="icon icon-lg" id="settings-icon">
        <path d="..." fill-rule="evenodd"></path>
    </svg>
    Settings
</a>

If you have attributes to declare but no additional class, you can pass an associative array as the second parameter instead:

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

<!-- Renders.. -->
<a href="/settings">
    <svg class="icon" id="settings-icon">
        <path d="..." fill-rule="evenodd"></path>
    </svg>
    Settings
</a>

If you'd like to override the default class name, specify a class in the attributes array:

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

<!-- Renders.. -->
<a href="/settings">
    <svg class="overridden">
        <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#zondicon-cog"></use>
    </svg>
    Settings
</a>

If you'd like to add an attribute that needs no value, just specify it without a key:

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

<!-- Renders.. -->
<a href="/settings">
    <svg class="icon" data-foo>
        <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#zondicon-cog"></use>
    </svg>
    Settings
</a>

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

<a href="/settings">
    {{ svg_image('cog')->id('settings-icon')->dataFoo('bar')->dataBaz() }} Settings
</a>

<!-- Renders.. -->
<a href="/settings">
    <svg class="icon" id="settings-icon" data-foo="bar" data-baz>
        <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#zondicon-cog"></use>
    </svg>
    Settings
</a>

Using a spritesheet

I recommend just rendering icons inline because it's really simple, has a few advantages over spritesheets, and has no real disadvantages, but if you really want to use a spritesheet, who am I to stop you?

So if you'd rather use a sprite sheet instead of rendering your SVGs inline, start by configuring the path to your spritesheet in the blade-svg config file:

<?php

return [
    // ...
    'spritesheet_path' => 'resources/assets/svg/spritesheet.svg',
    // ...
];

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

<?php

return [
    // ...
    'sprite_prefix' => 'zondicon-',
    // ...
];

Next, set the inline option to false which will tell Blade SVG to render icons using the spritesheet by default instead of inlining the entire SVG:

<?php

return [
    // ...
    'inline' => false,
    // ...
];

Make sure you render the hidden sprite sheet somewhere at the end of any layouts that use SVGs by using the svg_spritesheet() helper:

<!-- layout.blade.php -->

<!DOCTYPE html>
<html lang="en">
    <head><!-- ... --></head>
    <body>
        <!-- ... -->

        {{ svg_spritesheet() }}
    </body>
</html>

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:

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

<!-- Renders.. -->
<a href="/settings">
    <svg class="icon icon-lg" data-foo>
        <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#zondicon-cog"></use>
    </svg>
    Settings
</a>

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

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

<!-- Renders.. -->
<a href="/settings">
    <svg class="icon icon-lg" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
        <path d="M3.938 6.497a6.958 6.958 0 0 0-.702 1.694L0 9v2l3.236.809c.16.6.398 1.169.702 1.694l-1.716 2.861 1.414 1.414 2.86-1.716a6.958 6.958 0 0 0 1.695.702L9 20h2l.809-3.236a6.96 6.96 0 0 0 1.694-.702l2.861 1.716 1.414-1.414-1.716-2.86a6.958 6.958 0 0 0 .702-1.695L20 11V9l-3.236-.809a6.958 6.958 0 0 0-.702-1.694l1.716-2.861-1.414-1.414-2.86 1.716a6.958 6.958 0 0 0-1.695-.702L11 0H9l-.809 3.236a6.96 6.96 0 0 0-1.694.702L3.636 2.222 2.222 3.636l1.716 2.86zM10 13a3 3 0 1 0 0-6 3 3 0 0 0 0 6z" fill-rule="evenodd">
        </path>
    </svg>
    Settings
</a>

tomhatzer/blade-svg-origin 适用场景与选型建议

tomhatzer/blade-svg-origin 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 04 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 tomhatzer/blade-svg-origin 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 tomhatzer/blade-svg-origin 我们能提供哪些服务?
定制开发 / 二次开发

基于 tomhatzer/blade-svg-origin 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 11
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 5
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 163
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-04-18