Skip to content

Commit

Permalink
Use Blade::directive instead of Blade::extend (#237)
Browse files Browse the repository at this point in the history
* Add blade directives; Remove use of require_once

* Update README for renamed lm_attrs

* Use constants correctly

* Update @lm_attrs syntax in examples

* Add deprecation warning
  • Loading branch information
Xiphoseer authored and dustingraham committed Dec 7, 2018
1 parent ca5f197 commit a301b61
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 64 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ __For Laravel 4.x, check [version 1.5.0](https://github.com/lavary/laravel-menu/
* [Advanced Usage](#advanced-usage)
+ [A Basic Example](#a-basic-example)
+ [Control Structure for Blade](#control-structure-for-blade)
- [@lm-attrs](#lm-attrs)
- [@lm_attrs](#lm_attrs)
+ [Attributes and Callback function of item](#attributes-and-callback-function-of-item)
* [Configuration](#configuration)
* [If You Need Help](#if-you-need-help)
Expand Down Expand Up @@ -1472,7 +1472,7 @@ To put the rendered menu in your application template, you can simply include `c

Laravel menu extends Blade to handle special layouts.

##### @lm-attrs
##### @lm_attrs

You might encounter situations when some of your HTML properties are explicitly written inside your view instead of dynamically being defined when adding the item; However you will need to merge these static attributes with your Item's attributes.

Expand Down Expand Up @@ -1501,7 +1501,7 @@ The view:

```php
@foreach($items as $item)
<li@lm-attrs($item) @if($item->hasChildren()) class="dropdown" @endif data-test="test" @lm-endattrs>
<li @lm_attrs($item) @if($item->hasChildren()) class="dropdown" @endif data-test="test" @lm_endattrs>
<a href="{!! $item->url !!}">{!! $item->title !!} </a>
@if($item->hasChildren())
<ul class="dropdown-menu">
Expand Down
60 changes: 58 additions & 2 deletions src/Lavary/Menu/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Lavary\Menu;

use Illuminate\Support\ServiceProvider as BaseServiceProvider;
use Illuminate\Support\Facades\Blade;

class ServiceProvider extends BaseServiceProvider
{
Expand All @@ -26,13 +27,68 @@ public function register()
});
}

// Patterns and Replace string for lm-attr
// Remove with next major version
const LM_ATTRS_PATTERN = '/(\s*)@lm-attrs\s*\((\$[^)]+)\)/';
const LM_ATTRS_REPLACE = '$1<?php $lm_attrs = $2->attr(); ob_start(); ?>';

// Patterns and Replace string for lm-endattr
// Remove with next major version
const LM_ENDATTRS_PATTERN = '/(?<!\w)(\s*)@lm-endattrs(\s*)/';
const LM_ENDATTRS_REPLACE = '$1<?php echo \Lavary\Menu\Builder::mergeStatic(ob_get_clean(), $lm_attrs); ?>$2';

/*
* Extending Blade engine. Remove with next major version
*
* @deprecated
* @return void
*/
protected function bladeExtensions()
{
Blade::extend(function ($view, $compiler) {
if (preg_match(self::LM_ATTRS_PATTERN, $view)) {
\Log::debug("laravel-menu: @lm-attrs/@lm-endattrs is deprecated. Please switch to @lm_attrs and @lm_endattrs");
}
return preg_replace(self::LM_ATTRS_PATTERN, self::LM_ATTRS_REPLACE, $view);
});

Blade::extend(function ($view, $compiler) {
return preg_replace(self::LM_ENDATTRS_PATTERN, self::LM_ENDATTRS_REPLACE, $view);
});
}

/*
* Adding custom Blade directives.
*/
protected function bladeDirectives()
{
/*
* Buffers the output if there's any.
* The output will be passed to mergeStatic()
* where it is merged with item's attributes
*/
Blade::directive('lm_attrs', function ($expression) {
return '<?php $lm_attrs = ' . $expression . '->attr(); ob_start(); ?>';
});

/*
* Reads the buffer data using ob_get_clean()
* and passes it to MergeStatic().
* mergeStatic() takes the static string,
* converts it into a normal array and merges it with others.
*/
Blade::directive('lm_endattrs', function ($expression) {
return '<?php echo \Lavary\Menu\Builder::mergeStatic(ob_get_clean(), $lm_attrs); ?>';
});
}

/**
* Bootstrap the application events.
*/
public function boot()
{
// Extending Blade engine
require_once 'blade/lm-attrs.php';
$this->bladeDirectives();
$this->bladeExtensions();

$this->loadViewsFrom(__DIR__.'/resources/views', 'laravel-menu');

Expand Down
56 changes: 0 additions & 56 deletions src/Lavary/Menu/blade/lm-attrs.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@foreach($items as $item)
<li@lm-attrs($item) @if($item->hasChildren()) class="nav-item dropdown" @endif @lm-endattrs>
<li @lm_attrs($item) @if($item->hasChildren()) class="nav-item dropdown" @endif @lm_endattrs>
@if($item->link) <a@lm-attrs($item->link) @if($item->hasChildren()) class="nav-link dropdown-toggle" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" @else class="nav-link" @endif @lm-endattrs href="{!! $item->url() !!}">
{!! $item->title !!}
@if($item->hasChildren()) <b class="caret"></b> @endif
Expand All @@ -9,9 +9,9 @@
@endif
@if($item->hasChildren())
<ul class="dropdown-menu">
@include(config('laravel-menu.views.bootstrap-items'),
@include(config('laravel-menu.views.bootstrap-items'),
array('items' => $item->children()))
</ul>
</ul>
@endif
</li>
@if($item->divider)
Expand Down

0 comments on commit a301b61

Please sign in to comment.