PHPackages                             swayok/laravel-blade-directives - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. swayok/laravel-blade-directives

ActiveLibrary[Templating &amp; Views](/categories/templating)

swayok/laravel-blade-directives
===============================

Additional Blade directives for Laravel

1.1(6y ago)1875[1 issues](https://github.com/swayok/laravel-blade-directives/issues)MITPHPPHP &gt;=7.1.3

Since Mar 30Pushed 6y ago1 watchersCompare

[ Source](https://github.com/swayok/laravel-blade-directives)[ Packagist](https://packagist.org/packages/swayok/laravel-blade-directives)[ RSS](/packages/swayok-laravel-blade-directives/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (1)Versions (5)Used By (0)

What is this
============

[](#what-is-this)

This package provides additional directived for Laravel Blade template engingine. Among directives are fully-functional partials, translations and [dot.js](http://olado.github.io/doT/index.html)constructions.

Installation
------------

[](#installation)

Add require to `composer.json` and run `composer update`

```
"require": {
    "swayok/laravel-blade-directives": "~1.0",
}

```

Configuration
-------------

[](#configuration)

### Service provider for Laravel 5.6+

[](#service-provider-for-laravel-56)

Automatically added via package auto-discovery.

### Service provider for Laravel &lt; 5.6

[](#service-provider-for-laravel--56)

Add `\LaravelBladeDirectives\LaravelBladeDirectivesServiceProvider::class` to your `config/app.php` into `providers` array

Shortcuts
---------

[](#shortcuts)

```
@breakpoint
// add breakpoint to view (allows you to debug compiled views)

@string($id, $parameters = [], $locale = null)
@trans($id, $parameters = [], $locale = null)
// {{ trans($id, $parameters, $locale) }}

@html($id, $parameters = [], $locale = null)
@unescaped($id, $parameters = [], $locale = null)
// {!! trans($id, $parameters, $locale) !!}

@key($array, $key, $default, $escaped = true)
// {{ array_get($array, $key, $default) }} - when $escaped === true
// {!! array_get($array, $key, $default) !!} - when $escaped === false

Examples:

@string('frontend.footer', ['year' => 2019], 'en')
@trans('frontend.footer', ['year' => 2019])
@html('frontend.lang.' . app()->locale())
@unescaped('frontend.app_name')

@key($settings, 'language', 'en')
// {{ array_get($settings, 'language', 'en') }}
@key($settings, 'language', 'en', false)
// {!! array_get($settings, 'language', 'en') !!}

```

Partials
--------

[](#partials)

The idea behind partials is possibility to use sub-template several times in a single blade template without using a `@view()` directive that require a view file to be created. Sometimes you do not need separate view file for simple template like table row. Partials also useful to make big template a bit cleaner.

Partials declared and used like sections with exception that partials receive only data you explicitely pass into `@yieldPartial` and some shared data provided by `$__env->getShared()` (`\Illuminate\View\Factory->getShared()`). Under the hood partial is a closure that renders some blade template using passed data. It does not create files and all code is placed inside current blade template.

### Declare partial

[](#declare-partial)

```
@partial('name')
    {{ $var1 }}
@endPartial

```

### Yield partial

[](#yield-partial)

```
@yieldPartial('name', ['var1' => 'this is partial'])
@yieldpartial('name', ['var1' => 'this is another partial'])

```

### Result

[](#result)

```
this is partial
this is another partial

```

Note: all directives have both cameCase and lowercase versions

[dot.js](http://olado.github.io/doT/index.html) directives
----------------------------------------------------------

[](#dotjs-directives)

It is a pain using dot.js inserts inside blade templates. You need to add `@` before each dot.js insert, also you cannot insert php code inside such inserts. Template becomes a real mess sometimes.

The idea with this directives is to make blade templates that contain dot.js inserts more clean and visually better. In addition there must be a possibility to insert some php code inside directive.

### Simple inserts

[](#simple-inserts)

```
@jsEcho(it.value)
// {{= it.value }}
@jsecho(it.value || 'default')
// {{= it.value || 'default' }}

@jsEchoEncoded(it.value)
// {{! it.value }}
@jsechoencoded(it.value || "default")
// {{! it.value || "default" }}

@jsEval(it._some_name = 'value')
// {{ it._some_name = 'value' }}
@jseval(it._some_name = "value")
// {{ it._some_name = "value" }}

@jsIf(it.value)
// {{? it.value }}
@jsif(it.value === 'example')
// {{? it.value === 'example' }}

@jsIfSimple(it.value)
// {{? it.value }}
@jsifsimple(it.value === 'example')
// {{? it.value === 'example' }}

@jsElseIf(it.value)
// {{?? it.value }}
@jselseif(it.value === 'example')
// {{?? it.value === 'example' }}

@jsElseIfSimple(it.value)
// {{?? it.value }}
@jselseifsimple(it.value === 'example')
// {{?? it.value === 'example' }}

@jsElse
@jselse
// {{??}}

@jsEndIf
@jsendif
// {{?}}

@jsForEach(it.array as key => value)
// {{~ it.array :value:key }}
@jsforeach(it.array as value)
// {{~ it.array :value }}

@jsEndForEach
@jsendforeach
// {{~}}

@jsPartial(partial_name_without_spaces)
@jspartial(partial_name_without_spaces)
// {{##def.partial_name_without_spaces:

@jsEndPartial
@jsendpartial
// #}}

@jsEchoPartial(partial_name_without_spaces)
@jsechopartial(partial_name_without_spaces)
// {{#def.partial_name_without_spaces}}

```

### Complex inserts with PHP code

[](#complex-inserts-with-php-code)

PHP code inside dot.js directive must begin with ``.` and end with `.``(the quote is the one that placed on tilda `~` button on keyboard and is called `backtick`).

Using backticks allows using normal quotes and double quotes without problems caused by incorrect quotes escaping. Also if you're using PHP Storm you can configure Code Style for backticks that differs from other quotes and shows dot.js directives in unique way (configs are in the next section).

Examples:

```
$phpVar = 'var_string'
SOME_CONSTANT = 'constant_string'
$phpNumber = 5

@jsIf(it.value === "` . $phpVar . `")
// {{? it.value === "var_string" }}
// Resulting php code in compiled template is:

@jsIf(it.value === '` . $phpVar . `')
// {{? it.value === 'var_string' }}
// Resulting php code in compiled template is:

@jsif(it.value === "` . SOME_CONSTANT . `")
// {{? it.value === "constant_string" }}

@jsIf(it.value >= ` . ($phpNumber - 1) . ` && it.value = 4 && it.value  Languages & Frameworks > PHP > Blade`. Untick `Use default settings`. Open `Directives` tab.

Each directive have next inputs:

- Name
- Has parameter
- Prefix
- Suffix

Note: directives with same configs are grouped in Names - you will need to create a record for each directive name.

```
Names: string, trans, html, unescaped
Has parameter: true
Prefix:

Names: key
Has parameter: true
Prefix:

Names: partial
Has parameter: true
Prefix:
