@snip('pr" /> @snip('pr" /> @snip('pr"/>

Blade Snip allows you to use parts of a blade template multiple times. Basically partials, but inline.

Related tags

Laravel blade-snip
Overview

Packagist version License

Blade Snip

Blade Snip allows you to use parts of a blade template multiple times. Basically partials, but inline:

<div class="products">
    @snip('product', ['image' => null])
        <div class="product">
            @if ($image)
                <img src="{{ $image }}">
            @endif
            <h1>Lorem Ipsum Dolor</h1>
            <p{{ number_format($price ?? rand(10, 100)) }}</p>
            <button>Add to Basket</button>
        </div>
    @endsnip
    @stick('product')
    @stick('product', ['image' => 'cheese.jpg'])
    @stick('product', ['image' => 'potato.jpg', 'price' => 120])
    {{-- or --}}
    @spread('product', 3)
</div>
@snip('content')
    <x-figure caption="Lorem ipsum dolor sit amet">
        <img src="photo.jpg">
    </x-figure>
@endsnip
@if ($link)
    <a href="{{ $link }}">@stick('content')</a>
@else
    @stick('content')
@endif
@snip('complex_thing')
    ...
@endsnip
<div class="mobile">
    <h2>{{ $name }}</div>
    <p>{{ $location }}</div>
    <div>@stick('complex_thing')</div>    
</div>
<table class="desktop">
    <tr>
        <th>{{ $name }}</td>
        <td>{{ $location }}</td>
        <td>@stick('complex_thing')</td>
    </tr>
</table>

Why?

I created this to use when prototyping page layouts. It's useful to have reusable blocks, but I don’t want to jump between multiple files, or don’t know exactly how those files should be structured yet.

This was primarily intended as a development tool, it’s usually best to break things down into actual partials or components once you’re done prototyping. That being said, there are other potential use cases.

Installation

Run the following command from your project root:

composer require jacksleight/blade-snip

Usage

Check the examples above.

Directives accept the following arguments:

  • @snip(string $name, ?array $defaults = [])@endsnip
    • Defines a new snip
  • @stick(string $name, ?array $data = [])
    • Includes a snip
  • @spread(string $name, int $count || array $datas)
    • Includes a snip multiple times
    • Also adds an $index variable to each iteration

Under the hood the @snip and @endsnip directives just wrap that code in a closure, then @stick and @spread call it. As they're closures they have their own variable scope, but variables defined in the template are included. Names can only contain alpha-numeric characters and underscores.

You might also like...
If you are beginner in WordPress plugin development or if you want to develop your own store product plugin you use this plugin
If you are beginner in WordPress plugin development or if you want to develop your own store product plugin you use this plugin

hirwa-products-plugin If you are beginner in WordPress plugin development or if you want to develop your own store product plugin you use this plugin

Is an Extension of Laravel View Class which compiles String Template on the fly. It automatically detects changes on your string template and recompiles it if needed.

Laravel-fly-view Is an Extension of Laravel View Class which compiles String Template on the fly. It automatically detects changes on your string temp

Allows you to use Twig seamlessly in Laravel

Allows you to use Twig seamlessly in Laravel. Requirements TwigBridge = 0.13 supports Twig 3. If you need Twig 1/2 support, use the 0.12 versions. In

Register for multiple Livestorm sessions from an external form. Practical use of Livestorm API with PHP/Javascript.

Livestorm Multi Session Registration Register for multiple Livestorm sessions from an external form. Practical use of Livestorm API with PHP/Javascrip

Livewire component that provides you with a modal that supports multiple child modals while maintaining state.
Livewire component that provides you with a modal that supports multiple child modals while maintaining state.

About LivewireUI Modal LivewireUI Modal is a Livewire component that provides you with a modal that supports multiple child modals while maintaining s

Livewire component that provides you with a modal that supports multiple child modals while maintaining state.
Livewire component that provides you with a modal that supports multiple child modals while maintaining state.

About Wire Elements Modal Wire Elements Modal is a Livewire component that provides you with a modal that supports multiple child modals while maintai

Laravel Seo package for Content writer/admin/web master who do not know programming but want to edit/update SEO tags from dashboard
Laravel Seo package for Content writer/admin/web master who do not know programming but want to edit/update SEO tags from dashboard

Laravel Seo Tools Laravel is becoming more and more popular and lots of web application are developing. In most of the web application there need some

A package to easily make use of Iconic icons in your Laravel Blade views.
A package to easily make use of Iconic icons in your Laravel Blade views.

Blade Iconic A package to easily make use of Iconic icons in your Laravel Blade views. For a full list of available icons see the SVG directory. Iconi

A package to easily make use of Simple Icons in your Laravel Blade views.
A package to easily make use of Simple Icons in your Laravel Blade views.

Blade Simple Icons A package to easily make use of Simple Icons in your Laravel Blade views. For a full list of available icons see the SVG directory.

Releases(0.1.0)
Owner
Jack Sleight
Web Developer
Jack Sleight
A Laravel package that allows you to use multiple ".env" files in a precedent manner. Use ".env" files per domain (multi-tentant)!

Laravel Multi ENVs Use multiple .envs files and have a chain of precedence for the environment variables in these different .envs files. Use the .env

Allyson Silva 48 Dec 29, 2022
My aim is to make a complete website that should have all the essential parts a website should have.

Gaming-Ninja I aim to make a complete website that should have all the essential parts a website should have. https://gamingninja-3399.000webhostapp.c

Anand Jaiswar 3 Nov 23, 2022
Adds a widget and REST endpoint for the purpose of displaying post revisions inline on the frontend.

Post History This widget allows visitors to easily diff posts against their earlier revisions, displaying diffs of HTML inline. It should be plug and

Human Made 4 Dec 12, 2022
This package allows you to render livewire components like a blade component, giving it attributes, slots etc

X-livewire This package allows you to render livewire components like a blade component, giving it attributes, slots etc. Assuming you wanted to creat

null 7 Nov 15, 2022
Blade UI Kit is a set of renderless components to utilise in your Laravel Blade views

Blade UI Kit is a set of renderless components to utilise in your Laravel Blade views. In all essence, it's a collection of useful utilities, connecting the dots between different parts of the TALL stack. It was made for Blade, Laravel's powerful templating engine.

Blade UI Kit 1.2k Jan 5, 2023
Create Laravel views (blade template) using 'php artisan' command-line interface

About LaraBit Have you ever wonder to create Laravel views (Blade Templates) using the same type of artisan commands that you usually use to create ne

Ragib MRB 5 Oct 15, 2021
An opinionated blade template formatter for Laravel that respects readability

blade-formatter An opinionated blade template formatter for Laravel that respects readability Online Demo Features Automatically Indents markup inside

Shuhei Hayashibara 277 Dec 26, 2022
A Laravel Code Generator based on your Models using Blade Template Engine

Laravel Code Generator is a PHP Laravel Package that uses Blade template engine to generate code for you. The difference between other code generators

Victor Yoalli 59 Dec 5, 2022
Shows the path of each blade file loaded in a template

Laravel Tracer Tracer shows the paths of all the Blade files that are loaded into your templates. This could be very convenient for a number of reason

Appstract 97 Oct 1, 2022
Snippets for blade template engine

Blade Snippets for Sublime Text Blade is a simple, yet powerful templating engine provided with Laravel PHP framework. These snippets works with blade

Alex Antonyuk 113 Jan 3, 2023