benjamincrozat/laravel-blade-sugar

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

3.0.1 2018-05-17 07:54 UTC

README

Build Status Latest Stable Version License Total Downloads

Laravel Blade Sugar

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

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:

<!-- Renders as <img src="http(s)://example.com/img/photo.jpg"> -->
<img src="@asset('img/photo.jpg')">

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

@secureAsset()

Renders an asset using HTTPS:

<!-- Renders as <img src="https://example.com/img/photo.jpg"> -->
<img src="@secureAsset('img/photo.jpg')">

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

@checked()

Automatically adds a checked attribute if the condition returns true.

<input type="checkbox" @checked($value === 'something')>

@gravatar()

Automatically displays a Gravatar from a given email address.

<img src="@gravatar($email)">

@markdown()

Automatically parses Markdown.

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

@pagination()

Display a paginator only if there is more than one page.

So you do this:

@pagination($articles)

Instead of this:

@if ($articles->hasMorePages())
    {{ $articles->links() }}
@endif

@route()

Just the route() helper wrapped in a Blade directive.

<a href="@route('articles.index')">Blog</a>
<a href="@route('articles.show', $article->slug)">{{ $article->title }}</a>
<a href="@route('articles.show', ['slug' => $article->slug])">{{ $article->title }}</a>

@selected()

Automatically adds a selected attribute if the condition returns true.

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

@storageUrl()

Generates a URL from any supported storage.

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

@url()

Just 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()

You can now do this:

@with('category', $article->category)
@with('username', $article->user->name)

by {{ $username }} in <a href="@route('article-categories.show', $category->slug)">{{ $category->name }}</a>

Instead of:

by {{ $article->user->name }} in <a href="@route('article-categories.show', $article->category->slug)">{{ $article->category->name }}</a>

License

WTFPL