Wordpress based Shortcodes for Laravel Framework with shared variables, debugbar integration, flexible configurations and other useful features.
Build powerful and simple layouts using shortcodes in the content or views like this:
[b]Bold text[/b]
[row]
[col md=8]
[posts_list types="post,gallery" show_tags="yes"]
[/col]
[col md=4]
[poll id="1"]
[user_info username="test_user" website="mywebsite.com" active="yes"]
[last_free_post title="Free Posts"]
[/col]
[/row]
Via Composer
$ composer require vedmant/laravel-shortcodes
Publish configuration.
php artisan vendor:publish --tag=shortcodes
It will publish configuration file shortcodes.php
, edit it as needed.
Shortcode class should extend abstract \Vedmant\LaravelShortcodes\Shortcode class.
This packages adds the make:shortcode
artisan command:
php artisan make:shortcode PostsListShortcode
Which generates a shortcode class in the app/Shortcodes
folder by default.
You can use AppServiceProvider::boot
method to register all needed shortcodes.
Using shortcode class:
Shortcodes::add('b', BShortcode::class);
Using shortcode classes in array, preferable for lots of shortcodes:
Shortcodes::add([
'a' => AShortcode::class,
'b' => BShortcode::class,
]);
Using closure:
Shortcodes::add('test', function ($atts, $content, $tag, $manager) {
return new HtmlString('<strong>some test shortcode</strong>');
});
By default this package extends the View
class to parse all shortcodes during views rendering.
This feature can be disabled in the config file: 'render_views' => false
.
For better performance with lots of views it's advised to disable views auto-render.
Also to enable / disable rendering shortcodes for a specific view you can use:
view('some-view')->withShortcodes();
// Or
view('some-view')->withoutShortcodes();
{{ Shortcodes::render('[b]bold[/b]') }}
@shortcodes
[b class="block"]Content[/b]
@endshortcodes
Or
@shortcodes('[b]bold[/b]')
<div class="some-block">
{{ shortcodes('[b]bold[/b]') }}
</div>
Occasionally, you may need to share a piece of data with all shortcodes that are rendered by your application.
You may do so using the shortode facade's share
method.
Typically, you should place calls to share in the controller, or within a service provider's boot method.
Shortcodes::share('post', $post);
Then you can get shared attributes in the shortcode class:
$post = $this->shared('post');
$allShared = $this->shared();
The $casts property on your shortcode class provides a convenient method of converting attributes to
common data types. The $casts property should be an array where the key is the name of the attribute
being cast and the value is the type you wish to cast the column to. The supported cast types are:
int
, integer
, real
, float
, double
, boolean
, array
(comma separated values) and date
.
class YourShortcode extends Shortcode
{
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'show_ids' => 'array',
];
}
Now the show_ids
attribute will always be cast to an array when you access it.
(array attributes are casted from comma separated string, eg. "1,2,3").
There is a simple way to validate attributes. Error messages will be rendered on the shortcode place. For convenients it will return attributes.
class YourShortcode extends Shortcode
{
/**
* Render shortcode
*
* @param string $content
* @return string
*/
public function render($content)
{
$atts = $this->validate([
'post_id' => 'required|numeric|exists:posts,id',
]);
//
}
}
There is a useful option to aviod server (500) error for whole page when one of shortocode has thrown an exception.
To enable it set 'throw_exceptions' => false,
in the shortcodes.php
config file.
This will render exception details in the place of a shortcode and will not crash whole page request with 500 error. It will still log exception to a log file and report to Sentry if it's integrated.
There can be hundreds of registered shortcodes and having a way to show documentation for all shortcodes is quite a good feature. There is simple method that will collect descriptions and attributes data from all registered shortcodes:
$data = Shortcodes::registeredData();
It returns Collection object with generated data that can be used to generate any help information.
This packages supports Laravel Debugbar
and adds a tab with detailed info about rendered shortcodes.
Integration can be disabled in the config file with option: 'debugbar' => false,
.
$ vendor/bin/phpunit
- Add custom widget for debugbar integration
- Create performance profile tests, optimize performance
Please see contributing.md for details and a todolist.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
MIT. Please see the license file for more information.