2

I installed graham-campbell/markdown, and it works in the controller. I would like to extend it's functionality to blade so I could use @markdown($variable) but can't figure out how to accomplish that.

This is how my AppServiceProvider's boot method looks like with the added blade directive.

public function boot()
{
    Schema::defaultStringLength(191);

    Blade::directive('markdown', function ($expression) {
        return "<?php echo Markdown::convertToHtml($expression); ?>";
    });
}

And in my view

@markdown($comment->comment)

But I"m getting the following error:

Class 'Markdown' not found (View: C:\xampp\htdocs\portfolio\portfolio\resources\views\blog.blade.php)

I've added the use at the top of AppServiceProvider file:

use GrahamCampbell\Markdown\Facades\Markdown;

And still the same error. I've even tried the following directive instead of the one I have posted previously:

Blade::directive('markdown', function ($expression) {
    return Markdown::convertToHtml($expression);
});

And although it's frowned upon, I've tried to inject the markdown class into the view

@inject('markdown', 'GrahamCampbell\Markdown\Facades\Markdown')

The error no longer shows, but it simply displays $comment->comment.

If I put @markdown(foo **this**) I get 'foo this' just like I would expect. How do I extract the contents of '$comment->comment' and submit it to be parsed by the markdown compiler?

Also, is it possible to do that without the Facades injection?

[EDIT]

I've solved my issue where it just prints $comment->comment. I've removed any changes to AppServiceProvider... I've removed that use statement and blade directive and just using the following in view

@inject('markdown', 'GrahamCampbell\Markdown\Facades\Markdown')

{!! $markdown::convertToHtml($comment->comment) !!}

But I'm still interesting in using the directive @markdown($variable) without the need for that injection.

ezw
  • 338
  • 2
  • 12

1 Answers1

2

The first line of code is correct except that you need to add {} instead of (), please refer to this answer.

so you need to type it like this:{$expression} instead of ($expression).

here as well a good tutorial on how to create a custom directive and you can check laracasts.

Hamza Mohamed
  • 1,373
  • 1
  • 12
  • 28
  • Thank you. That worked and for some reason it's finding that function without me injecting it into facade VIA view. I looked over that tutorial a few times over but failed to notice that detail before posting this question – ezw Mar 03 '18 at 05:37
  • glad i was helpful. – Hamza Mohamed Mar 03 '18 at 05:39
  • Weird, I removed those curly braces and went back to the way it was when it was giving errors and it's still working. I'm beyond confused right now. Even clearing cache doesn't change results – ezw Mar 03 '18 at 05:45
  • 1
    that is because your views are still cached, as Laravel keeps it all cached, if you are editing blades directives, you need to clear the variables to reflect the correct results, use `php artisan view:clear` – Hamza Mohamed Mar 03 '18 at 05:50