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

[2.x] Add @render() blade directiive #24

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ php artisan vendor:publish --tag="laravel_editorjs-views"
```

## Usage
Blade Directive
```bladehtml
@render($posts->body)
```

Facade
```php
use App\Models\Post;

Expand Down
11 changes: 11 additions & 0 deletions src/LaravelEditorJsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace AlAminFirdows\LaravelEditorJs;

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

class LaravelEditorJsServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -38,5 +39,15 @@ public function boot()
__DIR__ . '/../resources/views' => $this->app->resourcePath('views/vendor/laravel_editorjs'),
], 'laravel_editorjs-views');
}

/**
* Blade directive to render editor.js blocks.
*
* @param mixed $blocks The blocks of content to be rendered.
* @return string Rendered HTML content.
*/
Blade::directive('render', function ($blocks) {
return "<?php echo app('laravel-editorjs')->render($blocks); ?>";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lmottasin What about the exception?

render() method throws exception when it fails to render.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I found is that handling the exception is not a valid solution. The developer must validate the JSON before saving it to the database, so the current approach is fine.

});
}
}
Loading