\QuickApps\ShortcodeShortcodeParser

Provides methods for shortcode parsing.

Shortcodes looks as follow:

  1. Self-closing form:

    {my_shortcode attr1=val1 attr2=val2 ... /}

  2. Enclosed form:

    {my_shortcode attr1=val1 attr2=val2 ... } content {/my_shortcode}

Shortcodes can be escaped by using an additional { symbol, for instance:

{{ something }}
// this will actually prints `{ something }`

{{something} dummy {/something}}
// this will actually prints `{something} dummy {/something}`

Summary

Methods
Properties
Constants
parse()
strip()
escape()
enable()
disable()
cache()
dropCache()
No public properties found
No constants found
_list()
_getDefaultContext()
_regex()
_doShortcode()
_parseAttributes()
$_defaultContext
$_listeners
$_enabled
$_cache
N/A
No private methods found
No private properties found
N/A

Properties

$_defaultContext

$_defaultContext : object

Default context to use.

Type

object

$_listeners

$_listeners : array

Holds a list of all registered shortcodes.

Type

array

$_enabled

$_enabled : boolean

Parser status.

The parse() method will not work when set to false.

Type

boolean

$_cache

$_cache : array

Used internally.

Type

array

Methods

parse()

parse(string $text, object $context) : string

Look for shortcodes in the given $text.

Parameters

string $text

The content to parse

object $context

The context for \Cake\Event\Event::$subject, if not given an instance of this class will be used

Returns

string

strip()

strip(string $text) : string

Removes all shortcodes from the given content. Useful when converting a string to plain text.

Parameters

string $text

Text from which to remove shortcodes

Returns

string —

Content without shortcodes markers

escape()

escape(string $text) : string

Escapes all shortcodes from the given content.

Parameters

string $text

Text from which to escape shortcodes

Returns

string —

Content with all shortcodes escaped

enable()

enable() : void

Enables shortcode parser.

disable()

disable() : void

Globally disables shortcode parser.

The parser() method will not work when disabled.

cache()

cache(null|string $key, mixed $value) : mixed

Reads, writes or search internal class's cache.

Usages:

  • When reading if no cache key is found NULL will be returned. e.g. $null = static::cache('invalid-key');
  • When writing, this method return the value that was written. e.g. $value = static::cache('key', 'value');
  • Set both arguments to NULL to read the whole cache content at the moment. e.g. $allCache = static::cache()
  • Set key to null and value to anything to find the first key holding the given value. e.g. $key = static::cache(null, 'search key for this value'), if no key for the given value is found NULL will be returned.

Examples:

Writing cache:

static::cache('user_name', 'John');
// returns 'John'

static::cache('user_last', 'Locke');
// returns 'Locke'

Reading cache:

static::cache('user_name');
// returns: John

static::cache('unexisting_key');
// returns: null

static::cache();
// Reads the entire cache
// returns: ['user_name' => 'John', 'user_last' => 'Locke']

Searching keys:

static::cache(null, 'Locke');
// returns: user_last

static::cache(null, 'Unexisting Value');
// returns: null

Parameters

null|string $key

Cache key to read or write, set both $key and $value to get the whole cache information

mixed $value

Values to write into the given $key, or null indicates reading from cache

Returns

mixed

dropCache()

dropCache(string|null $key) : void

Drops the entire cache or a specific key.

Usage:

static::dropCache('user_cache'); // removes "user_cache" only
static::dropCache(); // removes every key

Parameters

string|null $key

Cache key to clear, if NULL the entire cache will be erased.

_list()

_list() : array

Returns a list of all registered shortcodes.

Returns

array

_getDefaultContext()

_getDefaultContext() : \QuickApps\View\View

Gets default context to use.

Returns

\QuickApps\View\View

_regex()

_regex() : string

Retrieve the shortcode regular expression for searching.

The regular expression combines the shortcode tags in the regular expression in a regex class.

The regular expression contains 6 different sub matches to help with parsing.

1 - An extra { to allow for escaping shortcodes: {{ something }} 2 - The shortcode name 3 - The shortcode argument list 4 - The self closing / 5 - The content of a shortcode when it wraps some content. 6 - An extra } to allow for escaping shortcode

Returns

string —

The shortcode search regular expression

_doShortcode()

_doShortcode(array $m) : string

Invokes shortcode lister method for the given shortcode.

Parameters

array $m

Shortcode as preg array

Returns

string

_parseAttributes()

_parseAttributes(string $text) : array

Looks for shortcode's attributes.

Attribute names are always converted to lowercase. Values are untouched.

Example:

{shortcode_name attr1="value1" aTTr2=value2 CamelAttr=Val1 /}

Produces:

[
    'attr1' => 'value1',
    'attr2' => 'value2',
    'camelattr' => 'Val1',
]

Parameters

string $text

The text where to look for shortcodes

Returns

array —

Associative array of attributes as tag_name => value