Shortcodes are a small piece of code, indicated by brackets like (this), that performs a dedicated function on your site. You can place it just about anywhere you’d like, and it will add a specific feature to your page, post, or other content. For example, you can use shortcodes to display galleries, videos, or even playlists.

Shortcodes

There are several default shortcodes available.

Name Parameters Description
entries fetch, id, options, field, default Fetch entry (or collection) or specific field.
registry get, id, default Fetch specific item from registry.
php Execute php code.
eval Eval expression.
calc expressions with values Calculate values.
strings append, prepend, after, afterLast, before, beforeLast, lower, upper, sort, wordsLimit, at, base64Decode, base64Encode, between, camel, capitalize, chars, charsFrequency, contains, containsAll, containsAny, count, crc32, countSubString, endsWith, finish, firstSegment, format, getEncoding, setEncoding, hash, increment, indexOf, indexOfLast,insert, isAlpha, isAlphanumeric, isAscii, isBase64, isBlank, isBoolean, isDigit, isEmail, isEmpty, isEqual, isFalse, isTrue, isHexadecimal, isHTML, isIP, isJson, isUpper, isLower, isMAC, isNumeric, isPrintable, isPunctuation, isUrl, isSimilar, isSerialized, kebab, lastSegment, length, limit, lines, md5, move, normalizeNewLines, normalizeSpaces, offsetExists, offsetGet, padBoth, padLeft, padRight, quotesToEntities, random, reduceSlashes, repeat, replace, replaceDashes, replaceFirst, replaceLast, replaceNonAlpha, replaceNonAlphanumeric, replacePunctuations, reverse, segment, segments, sha1, sha256, shuffle, similarity, snake, start, startsWith, stripQuotes, stripSpaces, studly, substr, trim, trimLeft, trimRight, trimSlashes, ucfirst, wordsCount, words, wordsFrequency Do strings modifications.
textile Parse textile text.
markdown Parse markdown text.
filesystem get, file Do filesystem manipulation.
var set, get, unset, delete Access global vars.
field set, get, unset, delete Access current entry fields.
tr values, locale Returns translation of a string. If no translation exists, the original string will be returned.
if expression Adds ability to use logical if conditions.
when expression Adds ability to use logical positive if conditions.
unless expression Adds ability to use logical negative if conditions.
uuid Generates UUID.
getBaseUrl Get the application base url.
getProjectUrl Get the application project url.
getBasePath Get the application base path.
getAbsoluteUrl Get the application absolute path.
getUriString Get the uri string.
urlFor routeName, data, queryParams Get the url for a named route.
You may ignore shortcodes processing by wrapping entry content in raw shortcode tag.

Shortocodes Details

entries

Fetch entry (or entries collection) or a specific field.

Examples

Fetch single entry

(entries fetch id:'blog')

Fetch secific field from a single entry

(entries fetch id:'blog' field:'title' default:'Blog')

Fetch entries collection

(entries fetch id:'blog' options:'collection=true')

Fetch entries collection with filtering options

(entries fetch id:'blog' options:'collection=true&filter[sort_by][key]=date&filter[sort_by][direction]=ASC')
registry

Get specific item from registry.

Examples

(registry get id:'flextype.manifest.name' default:'Flextype')
php

Execute php code.

Examples

(php)
  echo "Hello World!";
(/php)
eval

Eval expression.

Examples

(eval)
  1 + 1
(/eval)
calc

Calculate values.

Examples

(calc:'1+1')
strings

Do strings manipulation.

You may use the following parameters:

append, prepend, after, afterLast, before, beforeLast, lower, upper, sort, wordsLimit, at, base64Decode, base64Encode, between, camel, capitalize, chars, charsFrequency, contains, containsAll, containsAny, count, crc32, countSubString, endsWith, finish, firstSegment, format, getEncoding, setEncoding, hash, increment, indexOf, indexOfLast,insert, isAlpha, isAlphanumeric, isAscii, isBase64, isBlank, isBoolean, isDigit, isEmail, isEmpty, isEqual, isFalse, isTrue, isHexadecimal, isHTML, isIP, isJson, isUpper, isLower, isMAC, isNumeric, isPrintable, isPunctuation, isUrl, isSimilar, isSerialized, kebab, lastSegment, length, limit, lines, md5, move, normalizeNewLines, normalizeSpaces, offsetExists, offsetGet, padBoth, padLeft, padRight, quotesToEntities, random, reduceSlashes, repeat, replace, replaceDashes, replaceFirst, replaceLast, replaceNonAlpha, replaceNonAlphanumeric, replacePunctuations, reverse, segment, segments, sha1, sha256, shuffle, similarity, snake, start, startsWith, stripQuotes, stripSpaces, studly, substr, trim, trimLeft, trimRight, trimSlashes, ucfirst, wordsCount, words, wordsFrequency.

Examples

// foo
(strings lower)Foo(/strings)

// FOO
(strings upper)foo(/strings)

// HELLO WORLD
(strings preppend: 'Hello ' upper)World(/strings)

// ...
textile

Parse textile text.

Examples

(textile)
  h2. Textile
  * is a _shorthand syntax_ used to generate valid HTML
  * is *easy* to read and *easy* to write
  * can generate complex pages, including: headings, quotes, lists, tables and figures
(/textile)
markdown

Parse markdown text.

Examples

(markdown)
  # Markdown
  ** markdown text **
(/markdown)
filesystem

Do filesystem manipulation.

Examples

(filesystem get file:'file.txt')
var

Access to global vars.

Examples

(var:foo)
(var get:foo)
(var set:foo value:Foo)
(var set:foo) Foo (/var)
field

Access current entry fields.

Examples

(field:title)
(field get:foo default:Foo)
(field get:foo) Default (/field)
(field set:foo value:Foo)
(field set:foo) Foo (/field)
(field unset:foo)
(field delete:foo)
tr

Returns translation of a string. If no translation exists, the original string will be returned.

Examples

(tr:'translate_key')
if

Adds ability to use logical if conditions.

Examples

(if:'100 > 20') 
  Display content here...
(/if)
when

Adds ability to use logical positive if conditions.

Examples

(when:'100 > 20') 
  Display content here...
(/when)
unless

Adds ability to use logical negative if conditions.

Examples

(unless:'100 < 20') 
  Display content here...
(/unless)
uuid

Generates UUID.

Examples

(uuid)
(uuid:4)
getBasePath

Get the application base path.

Examples

(getBaseUrl)
getBaseUrl

Get the application base path.

Examples

(getBaseUrl)
getProjectUrl

Get the application project url.

Examples

(getProjectUrl)
getAbsoluteUrl

Get the application absolute url.

Examples

(getAbsoluteUrl)
getUriString

Get uri string.

Examples

(getAbsoluteUrl)
urlFor

Get the url for a named route.

Examples

(urlFor routeName: 'route-name' data: '{"foo": "Foo"}' queryParams: '{"foo": "Foo"}')

Methods

Name Description
addHandler Add shortcode handler.
addEventHandler Add event handler.
process Processes text and replaces shortcodes.
parse Parses text into shortcodes.
getCacheID Get Cache ID for shortcode.

Methods Details

addHandler

Add shortcode handler.

/**
 * Add shortcode handler.
 *
 * @param string   $name    Shortcode
 * @param callable $handler Handler
 *
 * @access public
 */
public function addHandler(string $name, callable $handler)

Examples

parsers()->shortcodes()->addHandler('message', function () {
    return "Indeed. – Teal'c";
});
addEventHandler()

Add event handler.

/**
 * Add event handler.
 *
 * @param string   $name    Event
 * @param callable $handler Handler
 *
 * @access public
 */
public function addEventHandler(string $name, callable $handler)

Examples

parsers()->shortcodes()->addHandler('raw', static function (ShortcodeInterface $s) {
    return $s->getContent();
});

parsers()
    ->shortcodes()
    ->addEventHandler(Events::FILTER_SHORTCODES,
                      new FilterRawEventHandler(['raw']));

More details about events: github.com/thunderer/Shortcode#events

process

Processes text and replaces shortcodes.

/**
 * Processes text and replaces shortcodes.
 *
 * @param string $input A text containing SHORTCODE
 * @param bool   $cache Cache result data or no. Default is true
 *
 * @access public
 */
public function process(string $input, bool $cache = true)

Examples

$shortcode = 'text with (message)';

$html = parsers()->shortcodes()->process($shortcode);
parse

Parses text into shortcodes.

/**
 * Parses text into shortcodes.
 *
 * @param string $input A text containing SHORTCODE
 *
 * @access public
 */
public function parse(string $input)

Examples

$shortcode = 'text with (message)';

$text = parsers()->shortcodes()->parse($shortcode);
getCacheID

Get Cache ID for shortcode.

/**
 * Get Cache ID for shortcode.
 *
 * @param  string $input Input.
 * @param  string $string String to append to the Cache ID.
 * 
 * @return string Cache ID.
 *
 * @access public
 */
public function getCacheID(string $input, string $string = ''): string

Examples

$shortcode = 'text with (message)';

$cache_id = parsers()->shortcodes()->getCacheID($shortcode);