|
1 | 1 | # Shortcode |
2 | | -The Shortcode class for creating shortcodes. |
| 2 | + |
| 3 | +The Shortcode Class 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. |
| 4 | + |
| 5 | +Examples of shortcode tags: |
| 6 | + |
| 7 | +```php |
| 8 | +{{shortcode}} |
| 9 | +{{shortcode parameter="value"}} |
| 10 | +``` |
| 11 | + |
| 12 | +Example of escaping shortcodes: |
| 13 | +```php |
| 14 | +{{{shortcode}}} |
| 15 | +``` |
| 16 | + |
| 17 | +### Add new shortcode |
| 18 | + |
| 19 | +Your shorcode function: |
| 20 | +```php |
| 21 | +function returnSiteUrl() { |
| 22 | + return 'http://example.org'; |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +Add shortcode |
| 27 | +```php |
| 28 | +Shortcode::add('site_url', 'returnSiteUrl'); |
| 29 | +``` |
| 30 | + |
| 31 | +### Add new shortcode with Variables |
| 32 | +Your shorcode function: |
| 33 | +```php |
| 34 | +function foo($attributes) { |
| 35 | + // Extract attributes |
| 36 | + extract($attributes); |
| 37 | + |
| 38 | + // text |
| 39 | + if (isset($text)) $text = $text; else $text = ''; |
| 40 | + |
| 41 | + // return |
| 42 | + return $text; |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +Add shortcode {foo text="Hello World"} |
| 47 | +```php |
| 48 | +Shortcode::add('foo', 'foo'); |
| 49 | +``` |
| 50 | +Usage: |
| 51 | +```php |
| 52 | +{foo text="Hello World"} |
| 53 | +``` |
| 54 | +Result: |
| 55 | +``` |
| 56 | +Hello World |
| 57 | +``` |
| 58 | + |
| 59 | +### Add new shortcode with Variables and Content |
| 60 | + |
| 61 | +Your shorcode function: |
| 62 | +```php |
| 63 | +function foo($attributes, $content) { |
| 64 | + // Extract attributes |
| 65 | + extract($attributes); |
| 66 | + |
| 67 | + // text |
| 68 | + if (isset($color)) $color = $color; else $color = 'black'; |
| 69 | + |
| 70 | + // return |
| 71 | + return '<span style="color:'.$color.'">'.$content.'</span>'; |
| 72 | +} |
| 73 | +```php |
| 74 | +Add shortcode {foo color="red"} |
| 75 | +``` |
| 76 | +Shortcode::add('foo', 'foo'); |
| 77 | +```php |
| 78 | +Usage: |
| 79 | +``` |
| 80 | +{foo color="red"}Hello World{/foo} |
| 81 | +```html |
| 82 | +Result: |
| 83 | +<span style="color: red">Hello World</span> |
| 84 | + |
| 85 | +### Check if a shortcode has been registered. |
| 86 | +```php |
| 87 | +if (Shortcode::exists('foo')) { |
| 88 | + // do something... |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +### Remove a specific registered shortcode. |
| 93 | +```php |
| 94 | +Shortcode::delete('foo'); |
| 95 | +``` |
| 96 | + |
| 97 | +### Remove all registered shortcodes. |
| 98 | +```php |
| 99 | +Shortcode::clear(); |
| 100 | +``` |
| 101 | + |
| 102 | +## Braces |
| 103 | +The shortcode parser does not accept braces within attributes. Thus the following will fail: |
| 104 | +```php |
| 105 | +{foo attribute="{Some value}"}Hello World{/foo} |
| 106 | +``` |
3 | 107 |
|
4 | 108 | ## License |
5 | 109 | See [LICENSE](https://github.com/force-components/Shortcode/blob/master/LICENSE) |
0 commit comments