Laravel Blade SVG – Use SVGs in Blade templates

Laravel Blade SVG is a package that enables you to easily include and manipulate SVG files directly within your Blade templates. This package simplifies the process of using SVG images in your Laravel applications by providing a clean and expressive API.

 

Key Features

  • Simplified SVG Inclusion: Load SVG files directly in your Blade views without additional HTML.
  • Inline SVG Rendering: Allows for the SVG content to be included inline, enabling easy manipulation with CSS and JavaScript.
  • Configuration Options: Customize how SVGs are included and rendered.
  • Cache Support: Optionally cache the rendered SVGs for improved performance.

 

Installation

You can install the package via Composer:

bash
 

Configuration

After installation, you may publish the configuration file to customize settings as needed:

bash

This command will create a configuration file at config/blade-svg.php, where you can customize various settings, such as the SVG directory.

Using SVGs in Blade Templates

Basic Usage

Once the package is installed, you can include SVG files in your Blade templates using the @svg directive:

blade

For example, if you have an SVG file located at resources/svg/logo.svg, you can include it like this:

blade
 

Passing Attributes

You can also pass attributes to the SVG to customize its appearance, such as width, height, and classes:

blade

This will add the specified attributes to the SVG element, allowing you to style or resize it as needed.

Example of Inline SVG Rendering

Here’s a simple example of how to include an SVG and apply styles:

blade

In your CSS, you could then target the SVG:

css
 

Caching SVGs

To improve performance, you can enable caching for your SVGs in the configuration file. By default, caching is disabled. You can enable it by setting cache to true in config/blade-svg.php.

 

Conclusion

Laravel Blade SVG makes it easy to work with SVG images in your Laravel applications, providing a straightforward way to include, manipulate, and customize SVGs directly in your Blade templates. This package enhances the flexibility and maintainability of your front-end assets.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top