benjamincrozat/laravel-blade-sugar

Some useful sugar for your Laravel Blade templates to keep 'em DRY and clean.

4.0.4 2018-07-26 08:54 UTC

README

Build Status Latest Stable Version License Total Downloads

Laravel Blade Sugar

Add syntaxic sugar to Laravel Blade to keep your templates DRY and clean.

Requirements

This package is tested to run on PHP 5.6+ and Laravel 5.4+.

Installation

composer require benjamincrozat/laravel-blade-sugar

Usage

If you're on Laravel 5.4 or older, add the service provider in your config/app.php file:

'providers' => [

    BC\Laravel\BladeSugar\ServiceProvider::class,

],

Available directives

@asset()

Renders an asset using the current URL scheme:

<img src="@asset('img/photo.jpg')">

More in the official Laravel documentation: https://laravel.com/docs/helpers#method-asset

@secureAsset()

Renders an asset using HTTPS:

<img src="@secureAsset('img/photo.jpg')">

More in the official Laravel documentation: https://laravel.com/docs/helpers#method-secure-asset

@checked()

Automatically adds a checked attribute if your condition returns true.

<input type="radio" @checked('something' === $value)> <label>Choice #1</label>
<input type="radio" @checked('something' === $value)> <label>Choice #2</label>
<input type="radio" @checked('something' === $value)> <label>Choice #3</label>

@gravatar()

Automatically displays a Gravatar from a given email address.

<img src="@gravatar('homer@simpson.com')">

@markdown()

Renders Markdown using Parsedown, which is built in Laravel.

@markdown('**Hello, World!**')

@mix()

Echos out the path to your Laravel Mix generated asset.

<link rel="stylesheet" src="@mix('/path/to/some/styles.css')">

@route()

The route() helper wrapped in a Blade directive.

<a href="@route('posts.index')">Blog</a>
<a href="@route('posts.show', $post)">{{ $post->title }}</a>
<a href="@route('posts.show', $post, true)">{{ $post->title }}</a>

@selected()

Adds a selected attribute if your condition returns true.

<select>
    <option value="draft" @selected($post->status === 'draft')>Draft</option>
    <option value="published" @selected($post->status === 'published')>Published</option>
</select>

@storageUrl()

Generates a URL from any supported storage.

<img src="@storageUrl($article->illustration)">
<img src="@storageUrl($article->illustration, 's3')">

@title()

Generates a title tag depending on the arguments you pass.

<!-- Generates `<title>My Page Title</title>` -->
@title('My Page Title')

<!-- Generates `<title>Default Title</title>` -->
@title(null, 'Default Title')

@url()

The url() helper wrapped in a Blade directive.

<a href="@url('user/profile')">Register</a>
<a href="@url('user/profile', 'john-doe')">Register</a>
<a href="@url('some/other/route', [
    'foo' => 'bar',
    ...
])">Register</a>

@with()

This directive let you assign new variables inside your Blade template.

@with('foo', 'bar')

{{ $foo }}

Instead of:

<?php $foo = 'bar'; ?>

{{ $foo }}

License

WTFPL