Extension

Type
Class
Namespace
craft\web\twig
Inherits
craft\web\twig\Extension » Twig\Extension\AbstractExtension
Implements
Twig\Extension\GlobalsInterface
Since
3.0.0

Class Extension

View source (opens new window)

# Protected Properties

# environment

Type
\Twig\Environment, null (opens new window)
Default value
null

View source (opens new window)

# view

Type
craft\web\View, null (opens new window)
Default value
null

View source (opens new window)

# Public Methods

Method Description
__construct() Constructor
appendFilter() Appends HTML to the end of the given tag.
atomFilter() Converts a date to the Atom format.
attrFilter() Modifies a HTML tag’s attributes, supporting the same attribute definitions as craft\helpers\Html::renderTagAttributes() (opens new window).
camelFilter() CamelCases a string.
cloneFunction() Returns a clone of the given variable.
dataUrlFunction() Generates a base64-encoded data URL (opens new window) for the given file path or asset.
dateFilter() Extending Twig's
datetimeFilter() Formats the value as a date+time.
encencFilter() Encrypts and base64-encodes a string.
expressionFunction()
filterFilter() Filters an array.
getCsrfInput()
getFilters()
getFootHtml()
getFunctions()
getGlobals()
getHeadHtml()
getNodeVisitors()
getTests()
getTokenParsers()
gqlFunction() Executes a GraphQL query against the full schema.
groupFilter() Groups an array by a the results of an arrow function, or value of a property.
indexOfFilter() Returns the index of an item in a string or array, or -1 if it cannot be found.
jsonEncodeFilter() This method will JSON encode a variable. We're overriding Twig's default implementation to set some stricter encoding options on text/html/xml requests.
kebabFilter() Kebab-cases a string.
lcfirstFilter() Lowercases the first character of a multibyte string.
literalFilter() Escapes commas and asterisks in a string so they are not treated as special characters in craft\helpers\Db::parseParam().
markdownFilter() Parses text through Markdown.
mergeFilter() Merges an array with another one.
multisortFilter() Duplicates an array and sorts it with craft\helpers\ArrayHelper::multisort() (opens new window).
parseAttrFilter() Parses an HTML tag to find its attributes.
parseRefsFilter() Parses a string for reference tags.
pascalFilter() PascalCases a string.
pluginFunction() Returns a plugin instance by its handle.
prependFilter() Prepends HTML to the beginning of given tag.
purifyFilter() Purifies the given HTML using HTML Purifier.
pushFilter() Pushes one or more items onto the end of an array.
renderObjectTemplate()
replaceFilter() Replaces Twig's
roundFunction() Rounds the given value.
rssFilter() Converts a date to the RSS format.
seqFunction() Returns the next number in a given sequence, or the current number in the sequence.
shuffleFunction() Shuffles an array.
snakeFilter() Snake_cases a string.
svgFunction() Returns the contents of a given SVG file.
tagFunction() Generates a complete HTML tag.
timeFilter() Formats the value as a time.
translateFilter() Translates the given message.
truncateFilter() Truncates the string to a given length, while ensuring that it does not split words.
ucfirstFilter() Uppercases the first character of a multibyte string.
ucwordsFilter() Uppercases the first character of each word in a string.
unshiftFilter() Prepends one or more items to the beginning of an array.
withoutFilter() Returns an array without certain values.
withoutKeyFilter() Returns an array without a certain key.

# __construct()

Constructor

View source (opens new window)

Arguments

# appendFilter()

Since
3.3.0

Appends HTML to the end of the given tag.

View source (opens new window)

Arguments

Returns

string (opens new window) – The modified HTML

# atomFilter()

Converts a date to the Atom format.

View source (opens new window)

Arguments

Returns

string (opens new window) – The formatted date

# attrFilter()

Since
3.3.0

Modifies a HTML tag’s attributes, supporting the same attribute definitions as craft\helpers\Html::renderTagAttributes() (opens new window).

View source (opens new window)

Arguments

Returns

string (opens new window) – The modified HTML tag.

# camelFilter()

CamelCases a string.

View source (opens new window)

Arguments

  • $string (mixed) – The string

Returns

string (opens new window)

# cloneFunction()

Returns a clone of the given variable.

View source (opens new window)

Arguments

  • $var (mixed)

Returns

mixed

# dataUrlFunction()

Since
3.5.13

Generates a base64-encoded data URL (opens new window) for the given file path or asset.

View source (opens new window)

Arguments

Returns

string (opens new window) – The data URL

Throws

# dateFilter()

Extending Twig's |date filter so we can run any translations on the output.

View source (opens new window)

Arguments

Returns

mixed, string (opens new window)

# datetimeFilter()

Formats the value as a date+time.

View source (opens new window)

Arguments

Returns

mixed, string (opens new window)

# encencFilter()

Encrypts and base64-encodes a string.

View source (opens new window)

Arguments

  • $str (mixed) – The string

Returns

string (opens new window)

# expressionFunction()

Since
3.1.0

View source (opens new window)

Arguments

  • $expression (mixed)
  • $params (mixed)
  • $config (mixed)

Returns

yii\db\Expression (opens new window)

# filterFilter()

Filters an array.

View source (opens new window)

Arguments

Returns

array (opens new window)

# getCsrfInput()

DEPRECATED

Deprecated in Craft 3.0. Use csrfInput() instead.

View source (opens new window)

Returns

string (opens new window)

# getFilters()

View source (opens new window)

# getFootHtml()

DEPRECATED

Deprecated in Craft 3.0. Use endBody() instead.

View source (opens new window)

Returns

string (opens new window)

# getFunctions()

View source (opens new window)

# getGlobals()

View source (opens new window)

# getHeadHtml()

DEPRECATED

Deprecated in Craft 3.0. Use head() instead.

View source (opens new window)

Returns

string (opens new window)

# getNodeVisitors()

View source (opens new window)

# getTests()

View source (opens new window)

# getTokenParsers()

View source (opens new window)

# gqlFunction()

Since
3.3.12

Executes a GraphQL query against the full schema.

View source (opens new window)

Arguments

Returns

array (opens new window) – The query result

# groupFilter()

Groups an array by a the results of an arrow function, or value of a property.

View source (opens new window)

Arguments

Returns

array (opens new window)[] – The grouped items

Throws

  • \Twig\Error\RuntimeError
    if $arr is not of type array or Traversable

# indexOfFilter()

Returns the index of an item in a string or array, or -1 if it cannot be found.

View source (opens new window)

Arguments

  • $haystack (mixed)
  • $needle (mixed)

Returns

integer (opens new window)

# jsonEncodeFilter()

This method will JSON encode a variable. We're overriding Twig's default implementation to set some stricter encoding options on text/html/xml requests.

View source (opens new window)

Arguments

Returns

mixed – The JSON encoded value.

# kebabFilter()

Kebab-cases a string.

View source (opens new window)

Arguments

Returns

string (opens new window) – The kebab-cased string

# lcfirstFilter()

Lowercases the first character of a multibyte string.

View source (opens new window)

Arguments

  • $string (mixed) – The multibyte string.

Returns

string (opens new window) – The string with the first character converted to lowercase.

# literalFilter()

Escapes commas and asterisks in a string so they are not treated as special characters in craft\helpers\Db::parseParam().

View source (opens new window)

Arguments

  • $value (mixed) – The param value.

Returns

string (opens new window) – The escaped param value.

# markdownFilter()

Parses text through Markdown.

View source (opens new window)

Arguments

  • $markdown (mixed) – The markdown text to parse
  • $flavor (string (opens new window), null (opens new window)) – The markdown flavor to use. Can be 'original', 'gfm' (GitHub-Flavored Markdown), 'gfm-comment' (GFM with newlines converted to <br>s), or 'extra' (Markdown Extra). Default is 'original'.
  • $inlineOnly (boolean (opens new window)) – Whether to only parse inline elements, omitting any <p> tags.

Returns

string (opens new window)

# mergeFilter()

Since
3.4.0

Merges an array with another one.

View source (opens new window)

Arguments

Returns

array (opens new window) – The merged array

# multisortFilter()

Duplicates an array and sorts it with craft\helpers\ArrayHelper::multisort() (opens new window).

View source (opens new window)

Arguments

  • $array (mixed) – The array to be sorted. The array will be modified after calling this method.
  • $key (string (opens new window), Closure (opens new window), array (opens new window)) – The key(s) to be sorted by. This refers to a key name of the sub-array elements, a property name of the objects, or an anonymous function returning the values for comparison purpose. The anonymous function signature should be: function($item). To sort by multiple keys, provide an array of keys here.
  • $direction (integer (opens new window), array (opens new window)) – The sorting direction. It can be either SORT_ASC or SORT_DESC. When sorting by multiple keys with different sorting directions, use an array of sorting directions.
  • $sortFlag (integer (opens new window), array (opens new window)) – The PHP sort flag. Valid values include SORT_REGULAR, SORT_NUMERIC, SORT_STRING, SORT_LOCALE_STRING, SORT_NATURAL and SORT_FLAG_CASE. Please refer to PHP manual (opens new window) for more details. When sorting by multiple keys with different sort flags, use an array of sort flags.

Returns

array (opens new window) – The sorted array

Throws

# parseAttrFilter()

Since
3.4.0

Parses an HTML tag to find its attributes.

View source (opens new window)

Arguments

Returns

array (opens new window) – The parsed HTML tag attributes

Throws

# parseRefsFilter()

Parses a string for reference tags.

View source (opens new window)

Arguments

Returns

string (opens new window)

# pascalFilter()

PascalCases a string.

View source (opens new window)

Arguments

  • $string (mixed) – The string

Returns

string (opens new window)

# pluginFunction()

Since
3.1.0

Returns a plugin instance by its handle.

View source (opens new window)

Arguments

Returns

craft\base\PluginInterface, null (opens new window) – The plugin, or null if it's not installed

# prependFilter()

Since
3.3.0

Prepends HTML to the beginning of given tag.

View source (opens new window)

Arguments

Returns

string (opens new window) – The modified HTML

# purifyFilter()

Since
3.4.0

Purifies the given HTML using HTML Purifier.

View source (opens new window)

Arguments

Returns

string (opens new window) – The purified HTML

# pushFilter()

Since
3.5.0

Pushes one or more items onto the end of an array.

View source (opens new window)

Arguments

Returns

array (opens new window)

# renderObjectTemplate()

View source (opens new window)

Arguments

Returns

string (opens new window)

# replaceFilter()

Replaces Twig's |replace filter, adding support for passing in separate search and replace arrays.

View source (opens new window)

Arguments

  • $str (mixed)
  • $search (mixed)
  • $replace (mixed)

Returns

mixed

# roundFunction()

DEPRECATED

Deprecated in 3.0.0. Use Twig's |round filter instead.

Rounds the given value.

View source (opens new window)

Arguments

Returns

integer (opens new window), float (opens new window)

# rssFilter()

Converts a date to the RSS format.

View source (opens new window)

Arguments

Returns

string (opens new window) – The formatted date

# seqFunction()

Since
3.0.31

Returns the next number in a given sequence, or the current number in the sequence.

View source (opens new window)

Arguments

Returns

integer (opens new window), string (opens new window)

Throws

# shuffleFunction()

Shuffles an array.

View source (opens new window)

Arguments

  • $arr (mixed)

Returns

mixed

# snakeFilter()

Snake_cases a string.

View source (opens new window)

Arguments

  • $string (mixed) – The string

Returns

string (opens new window)

# svgFunction()

Returns the contents of a given SVG file.

View source (opens new window)

Arguments

Returns

string (opens new window)

# tagFunction()

Since
3.3.0

Generates a complete HTML tag.

View source (opens new window)

Arguments

  • $type (string (opens new window)) – The tag type ('p', 'div', etc.)
  • $attributes (array (opens new window)) – The HTML tag attributes in terms of name-value pairs. If text is supplied, the value will be HTML-encoded and included as the contents of the tag. If 'html' is supplied, the value will be included as the contents of the tag, without getting encoded.

Returns

string (opens new window)

# timeFilter()

Formats the value as a time.

View source (opens new window)

Arguments

Returns

mixed, string (opens new window)

# translateFilter()

Translates the given message.

View source (opens new window)

Arguments

Returns

string (opens new window) – The translated message.

# truncateFilter()

Since
3.5.10

Truncates the string to a given length, while ensuring that it does not split words.

View source (opens new window)

Arguments

Returns

string (opens new window) – The truncated string

# ucfirstFilter()

Uppercases the first character of a multibyte string.

View source (opens new window)

Arguments

  • $string (mixed) – The multibyte string.

Returns

string (opens new window) – The string with the first character converted to upercase.

# ucwordsFilter()

Uppercases the first character of each word in a string.

View source (opens new window)

Arguments

Returns

string (opens new window)

# unshiftFilter()

Since
3.5.0

Prepends one or more items to the beginning of an array.

View source (opens new window)

Arguments

Returns

array (opens new window)

# withoutFilter()

Returns an array without certain values.

View source (opens new window)

Arguments

  • $arr (mixed)
  • $exclude (mixed)

Returns

array (opens new window)

# withoutKeyFilter()

Since
3.2.0

Returns an array without a certain key.

View source (opens new window)

Arguments

Returns

array (opens new window)