Shortcode Component
, (*1)
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., (*2)
Installation
composer require flextype-components/shortcode
Usage
use Flextype\Component\Shortcode\Shortcode;
Examples of shortcode tags:, (*3)
{{shortcode}}
{{shortcode parameter="value"}}
Example of escaping shortcodes:, (*4)
{{{shortcode}}}
Add new shortcode
Your shorcode function:, (*5)
function returnSiteUrl() {
return 'http://example.org';
}
Add shortcode, (*6)
Shortcode::add('site_url', 'returnSiteUrl');
Add new shortcode with Variables
Your shorcode function:, (*7)
function foo($attributes) {
// Extract attributes
extract($attributes);
// text
if (isset($text)) $text = $text; else $text = '';
// return
return $text;
}
Add shortcode {foo text="Hello World"}, (*8)
Shortcode::add('foo', 'foo');
Usage:, (*9)
{foo text="Hello World"}
Result:, (*10)
Hello World
Add new shortcode with Variables and Content
Your shorcode function:, (*11)
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"}, (*12)
Shortcode::add('foo', 'foo');
Usage:, (*13)
{foo color="red"}Hello World{/foo}
Result:, (*14)
<span style="color: red">Hello World</span>
Check if a shortcode has been registered.
if (Shortcode::exists('foo')) {
// do something...
}
Remove a specific registered shortcode.
Shortcode::delete('foo');
Remove all registered shortcodes.
Shortcode::clear();
Braces
The shortcode parser does not accept braces within attributes. Thus the following will fail:, (*15)
{foo attribute="{Some value}"}Hello World{/foo}
License
See LICENSE, (*16)