The Shortcode Component is a simple regex based parser that allows you to replace simple bbcode-like tags within a HTMLText or HTMLVarchar field when rendered into a content.
composer require flextype-components/shortcode
use Flextype\Component\Shortcode\Shortcode;
Examples of shortcode tags:
{{shortcode}}
{{shortcode parameter="value"}}
Example of escaping shortcodes:
{{{shortcode}}}
Your shorcode function:
function returnSiteUrl() {
return 'http://example.org';
}
Add shortcode
Shortcode::add('site_url', 'returnSiteUrl');
Your shorcode function:
function foo($attributes) {
// Extract attributes
extract($attributes);
// text
if (isset($text)) $text = $text; else $text = '';
// return
return $text;
}
Add shortcode {foo text="Hello World"}
Shortcode::add('foo', 'foo');
Usage:
{foo text="Hello World"}
Result:
Hello World
Your shorcode function:
function foo($attributes, $content) {
// Extract attributes
extract($attributes);
// text
if (isset($color)) $color = $color; else $color = 'black';
// return
return '<span style="color:'.$color.'">'.$content.'</span>';
}
Add shortcode {foo color="red"}
Shortcode::add('foo', 'foo');
Usage:
{foo color="red"}Hello World{/foo}
Result:
<span style="color: red">Hello World</span>
if (Shortcode::exists('foo')) {
// do something...
}
Shortcode::delete('foo');
Shortcode::clear();
The shortcode parser does not accept braces within attributes. Thus the following will fail:
{foo attribute="{Some value}"}Hello World{/foo}
See LICENSE