Package Data | |
---|---|
Maintainer Username: | patrickbrouwers |
Maintainer Contact: | patrickbrouwers@live.nl (Patrick Brouwers) |
Package Create Date: | 2014-07-12 |
Package Last Update: | 2017-02-07 |
Language: | PHP |
License: | BSD-3-Clause |
Last Refreshed: | 2024-11-22 03:07:18 |
Package Statistics | |
---|---|
Total Downloads: | 12,656 |
Monthly Downloads: | 9 |
Daily Downloads: | 0 |
Total Stars: | 20 |
Total Watchers: | 1 |
Total Forks: | 4 |
Total Open Issues: | 1 |
Wordpress like shortcodes for Laravel 4.2.
[b class="bold"]Bold text[/b]
[tabs]
[tab]Tab 1[/tab]
[tab]Tab 2[/tab]
[/tabs]
[user id="1" display="name"]
If you are looking for BBcodes, see: https://github.com/patrickbrouwers/Laravel-BBcodes
#Installation
Require this package in your composer.json
and update composer.
"brouwers/shortcodes": "1.*"
After updating composer, add the ServiceProvider to the providers array in app/config/app.php
'Brouwers\Shortcodes\ShortcodesServiceProvider',
You can use the facade for shorter code. Add this to your aliases:
'Shortcode' => 'Brouwers\Shortcodes\Facades\Shortcode',
The class is bound to the ioC as shortcode
$shortcode = App::make('shortcode');
By default shortcode compiling is set to false inside the config.
To enable the view compiling features:
return View::make('view')->withShortcodes();
This will enable shortcode rendering for that view only.
Enabeling the shortcodes through config shortcodes::enabled
will enable shortcoding rendering for all views.
Shortcode::enable();
Shortcode::disable();
With the config set to true, you can disable the compiling per view.
return View::make('view')->withoutShortcodes();
To use default compiling:
Shortcode::compile($contents);
Inside a file or service provider you can register the shortcodes. (E.g. app/start/shortcodes.php
or App/Services/ShortcodeServiceProvider.php
)
Shortcodes can be registered like Laravel macro's with a callback:
Shortcode::register('b', function($shortcode, $content, $compiler, $name)
{
return '<strong class="'. $shortcode->class .'">' . $content . '</strong>';
});
class BoldShortcode {
public function register($shortcode, $content, $compiler, $name)
{
return '<strong class="'. $shortcode->class .'">' . $content . '</strong>';
}
}
Shortcode::register('b', 'BoldShortcode');
class BoldShortcode {
public function custom($shortcode, $content, $compiler, $name)
{
return '<strong class="'. $shortcode->class .'">' . $content . '</strong>';
}
}
Shortcode::register('b', 'BoldShortcode@custom');
If you only want to show the html attribute when the attribute is provided in the shortcode, you can use $shortcode->get($attributeKey, $fallbackValue = null)
class BoldShortcode {
public function register($shortcode, $content, $compiler, $name)
{
return '<strong '. $shortcode->get('class', 'default') .'>' . $content . '</strong>';
}
}