gornymedia / laravel-shortcodes
Laravel shortcodes package
Installs: 32 724
Dependents: 0
Suggesters: 0
Security: 0
Stars: 24
Watchers: 1
Forks: 4
Open Issues: 1
Requires
- php: >=7.1.3
This package is not auto-updated.
Last update: 2024-11-13 07:17:23 UTC
README
Laravel Shortcodes Package
Laravel compatibility
Installation
Require this package in your composer.json
and update composer.
"gornymedia/laravel-shortcodes": "^1.3",
Add the service provider to config/app.php
Gornymedia\Shortcodes\ShortcodesServiceProvider::class,
Add the class alias to config/app.php
'Shortcode' => Gornymedia\Shortcodes\Facades\Shortcode::class,
Usage
Creating shortcodes
Inside a service provider you can create the shortcodes with attributes.
use Gornymedia\Shortcodes\Facades\Shortcode; Shortcode::add('example', function($atts, $content, $name) { $a = Shortcode::atts([ 'name' => $name, 'foo' => 'something', ], $atts); return "foo = {$a['foo']}"; }); Usage : [example foo="something else"]
Include partial files in shortcode
use Gornymedia\Shortcodes\Facades\Shortcode; Shortcode::add('widget', function($atts, $content, $name) { $a = Shortcode::atts([ 'name' => $name, 'foo' => 'something' ], $atts); $file = 'partials/' . $a['name'] ; // ex: resource/views/partials/ $atts['name'] .blade.php if (view()->exists($file)) { return view($file, $a); } }); Usage : [widget name="maps"]
Compile shortcodes inside shortcode content
use Gornymedia\Shortcodes\Facades\Shortcode; Shortcode::add('strong', function($atts, $content, $name) { $content = Shortcode::compile($content); return "<strong>$content</strong>"; }); Usage: [strong][example][/strong]
View rendering
compileShortcodes()
To render the view and compile the Shortcodes:
return view('view')->compileShortcodes();
stripShortcodes()
To render the view and remove the Shortcodes
return view('view')->stripShortcodes();
License
This package is open-source software licensed under MIT License.
This package uses WordPress shortcodes methods. The license under which the WordPress software is released is the GPLv2 (or later).