Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.5] Blade : add else in the custom if directive #21611

Merged
merged 1 commit into from
Oct 11, 2017

Conversation

bmichotte
Copy link
Contributor

@bmichotte bmichotte commented Oct 11, 2017

This PR adds theelse in the custom if directive to blade.

Example use-case :

<?php
Blade::if('instanceof', function ($object, $class) {
    return $object instanceof $class;
});
?>

(this example can not work as it, but you have the idea)

Actually, we have to use the @instanceof this way :

@instanceof($object, App\User::class)
   You're a user
@endinstanceof

@instanceof($object, App\Product::class)
   You're a product
@else
  I don't know what you are !
@endinstanceof

This PR allows us to write the following

@instanceof($object, App\User::class)
   You're a user
@elseinstanceof($object, App\Product::class)
   You're a product
@else
   I don't know what you are !
@endinstanceof

Tests are provided.

@ludo237
Copy link

ludo237 commented Oct 11, 2017

This can be done by the user by extending blade itself

@bmichotte
Copy link
Contributor Author

@ludo237 The if also can be done by the user :)

Also, this is more convenient because we don't have to write this kind of directives

Blade::directive('instanceof', function($expression) {
      $exploded = explode(',', $expression);
      $object = trim(array_shift($exploded));
      $class = trim(array_shift($exploded));
      return "<?php if ($object instanceof $class): ?>";
});
Blade::directive('elseinstanceof', function($expression) {
      $exploded = explode(',', $expression);
      $object = trim(array_shift($exploded));
      $class = trim(array_shift($exploded));
      return "<?php elseif ($object instanceof $class): ?>";
});
Blade::directive('endinstanceof', function() {
      return "<?php endif; ?>";
});

@taylorotwell taylorotwell merged commit b9a3279 into laravel:5.5 Oct 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants