PHPackages                             periloso/laravel-blade-directives-extended - 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. periloso/laravel-blade-directives-extended

ActiveLibrary

periloso/laravel-blade-directives-extended
==========================================

Directive Extensions to Laravel Blade templating engine.

1.0.2(8y ago)2121MITPHPPHP &gt;=5.4.0

Since May 30Pushed 8y ago1 watchersCompare

[ Source](https://github.com/periloso/laravel-blade-directives-extended)[ Packagist](https://packagist.org/packages/periloso/laravel-blade-directives-extended)[ RSS](/packages/periloso-laravel-blade-directives-extended/feed)WikiDiscussions master Synced 6d ago

READMEChangelogDependencies (1)Versions (4)Used By (0)

Laravel Blade Directives Extended
=================================

[](#laravel-blade-directives-extended)

[![Latest Stable Version](https://camo.githubusercontent.com/a089e7c9291ca42380b2f961ff5788e4cbe4ab4ee71d064c45f7e544474e4f16/68747470733a2f2f706f7365722e707567782e6f72672f706572696c6f736f2f6c61726176656c2d626c6164652d646972656374697665732d657874656e6465642f762f737461626c652e737667)](https://packagist.org/packages/periloso/laravel-blade-directives-extended)[![License](https://camo.githubusercontent.com/62c511bea2d68fa5b1391ef9a527398348a4835306594eb94042b83a3466f677/68747470733a2f2f706f7365722e707567782e6f72672f706572696c6f736f2f6c61726176656c2d626c6164652d646972656374697665732d657874656e6465642f6c6963656e7365)](https://packagist.org/packages/periloso/laravel-blade-directives-extended)

This package adds custom directives for the Laravel 5 blade engine.

DirectiveDescription@activeIfRoute($path, 'class if', 'class else')Sets the class if the route is equal to the string.@activeUnlessRoute($path, 'class unless', 'class else')Sets the class if the route is different than the string.@activeIfRouteContains($path, 'class if', 'class else')Sets the class if the route contains the string.@activeUnlessRouteContains($path, 'class unless', 'class else')Sets the class unless the route contains the string.@activeIfRouteStartsWith($path, 'class if', 'class else')Sets the class if the route starts with the string.@activeUnlessRouteStartsWith($path, 'class unless', 'class else')Sets the class if the route does not start with the string.@activeIfRouteEndsWith($path, 'class if', 'class else')Sets the string if the route does not end with the string.@activeUnlessRouteEndsWith($path, 'class unless', 'class else')Sets the string unless the route does end with the string.@ifempty($array)Checks whether the array is empty.@endifemptyCloses the ifempty statement.@set($variable, value)Creating (declaring) PHP variables@dd($variable)Laravel dd() function.@explode($delimiter, $string)php explode() function.@implode($delimiter, $array)php implode() function.@vardump($variable)php var\_dump() function.@set($name, value)Sets a variable.@truncate('Your String' , 4)Truncates a variable.@csrf($namespace)Sets the csrf token to the browser's window object. The namespace is optional.@js(users, $users)Passes a variable to javascript, adding it to window.$variableName.@optional('overlay')Yields the content only if the yield is set.@endoptionalCloses the optional statement.@cache('my-cache-key')Starts a cacheable block given a cache key@cache($post)Starts a cacheable block given a model (must use Cacheable trait)@endcacheCloses a cacheable blockInstallation
------------

[](#installation)

Install the package by using Composer:

```
composer require "periloso/laravel-blade-directives-extended"
```

After updating composer, add the ServiceProvider to the providers array in config/app.php

```
Periloso\BladeDirectivesExtended\BladeDirectivesExtendedServiceProvider::class
```

**Important** - when extending Blade, it's necessary to clear the cached view!

```
php artisan view:clear
```

Usage example:
--------------

[](#usage-example)

```
@set($alpha, true)

	Laravel

    		@if($alpha)
                Welcome to Laravel!
            @endif

```

About the caching directive:
----------------------------

[](#about-the-caching-directive)

For this package to function properly, you must use a Laravel cache driver that supports tagging (like `Cache::tags('foo')`). Drivers such as Memcached and Redis support this feature.

Check your `.env` file, and ensure that your `CACHE_DRIVER` choice accomodates this requirement:

```
CACHE_DRIVER=memcached

```

> Have a look at [Laravel's cache configuration documentation](https://laravel.com/docs/5.2/cache#configuration), if you need any help.

### The Basics

[](#the-basics)

With the package now installed, you may use the provided `@cache` Blade directive anywhere in your views, like so:

```
@cache('my-cache-key')

        Hello World

@endcache
```

By surrounding this block of HTML with the `@cache` and `@endcache` directives, we're asking the package to cache the given HTML. Now this example is trivial, however, you can imagine a more complex view that includes various nested caches, as well as lazy-loaded relationship calls that trigger additional database queries. After the initial page load that caches the HTML fragment, each subsequent refresh will instead pull from the cache. As such, those additional database queries will never be executed.

Please keep in mind that, in production, this will cache the HTML fragment "forever." For local development, on the other hand, we'll automatically flush the relevant cache for you each time you refresh the page. That way, you may update your views and templates however you wish, without needing to worry about clearing the cache manually.

Now because your production server will cache the fragments forever, you'll want to add a step to your deployment process that clears the relevant cache.

```
Cache::tags('views')->flush();
```

### Caching Models

[](#caching-models)

While you're free to hard-code any string for the cache key, the true power of Russian-Doll caching comes into play when we use a timestamp-based approach.

Consider the following fragment:

```
@cache($post)

        {{ $post->title }}>
        Written By: {{ $post->author->username }}

        {{ $post->body }}

@endcache
```

In this example, we're passing the `$post` object, itself, to the `@cache` directive - rather than a string. The package will then look for a `getCacheKey()` method on the model. We've already done that work for you; just have your Eloquent model use the `Laracasts\Matryoshka\Cacheable` trait, like so:

```
use Laracasts\Matryoshka\Cacheable;

class Post extends Eloquent
{
    use Cacheable;
}
```

Alternatively, you may use this trait on a parent class that each of your Eloquent models extend.

That should do it! Now, the cache key for this fragment will include the object's `id` and `updated_at` timestamp: `App\Post/1-13241235123`.

> The key is that, because we factor the `updated_at` timestamp into the cache key, whenever you update the given post, the cache key will change. This will then, in effect, bust the cache!

#### Touching

[](#touching)

In order for this technique to work properly, it's vital that we have some mechanism to alert parent relationships (and subsequently bust parent caches) each time a model is updated. Here's a basic workflow:

1. Model is updated in the database.
2. Its `updated_at` timestamp is refreshed, triggering a new cache key for the instance.
3. The model "touches" (or pings) its parent.
4. The parent's `updated_at` timestamp, too, is updated, which busts its associated cache.
5. Only the affected fragments re-render. All other cached items remain untouched.

Luckily, Laravel offers this "touch" functionality out of the box. Consider a `Note` object that needs to alert its parent `Card` relationship each time an update occurs.

```
