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
addressFilter()
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.
collectFunction() Returns a new collection.
currencyFilter() Formats the value as a currency number.
dataUrlFunction() Generates a base64-encoded data URL (opens new window) for the given file path or asset.
dateFilter() Extending Twig's
dateFunction() Converts an input to a DateTime (opens new window) instance.
datetimeFilter() Formats the value as a date+time.
dumpFunction() Displays a variable(s).
encencFilter() Encrypts and base64-encodes a string.
expressionFunction()
filesizeFilter() Formats the value in bytes as a size in human-readable form, for example 12 kB.
filterFilter() Filters an array.
getFilters()
getFunctions()
getGlobals() Registers global variables.
getNodeVisitors()
getTests()
getTokenParsers()
gqlFunction() Executes a GraphQL query against the full schema.
groupFilter() Groups an array by the results of an arrow function, or value of a property.
httpdateFilter() Converts a date to the HTTP format (used by HTTP headers such as Expires).
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.
lengthFilter() Returns the length of an array, or the total result count of a query.
literalFilter() Escapes commas and asterisks in a string so they are not treated as special characters in craft\helpers\Db::parseParam().
mapFilter() Maps an array.
markdownFilter() Parses text through Markdown.
mergeFilter() Merges an array with another one.
moneyFilter() Outputs a value from a Money object.
multisortFilter() Duplicates an array and sorts it with craft\helpers\ArrayHelper::multisort() (opens new window).
numberFilter() Formats the value as a decimal number.
parseAttrFilter() Parses an HTML tag to find its attributes.
parseRefsFilter() Parses a string for reference tags.
pascalFilter() PascalCases a string.
percentageFilter() Formats the value as a percent number with "%" sign.
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.
reduceFilter() Reduces an array.
removeClassFilter() Removes a class (or classes) from the given HTML tag.
renderObjectTemplate()
replaceFilter() Replaces Twig's
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.
sortFilter() Sorts an array.
svgFunction() Returns the contents of a given SVG file.
tagFunction() Generates a complete HTML tag.
timeFilter() Formats the value as a time.
timestampFilter() Formats the value as a human-readable timestamp.
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.
widontFilter() Inserts a non-breaking space between the last two words of a string.
withoutFilter() Returns an array without certain values.
withoutKeyFilter() Returns an array without a certain key.

# __construct()

Constructor

View source (opens new window)

Arguments

# addressFilter()

Since
4.0.0

View source (opens new window)

Arguments

Returns

string (opens new window)

# 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

# collectFunction()

Since
4.0.0

Returns a new collection.

View source (opens new window)

Arguments

  • $var (mixed)

Returns

\Illuminate\Support\Collection

# currencyFilter()

Since
3.6.0

Formats the value as a currency number.

View source (opens new window)

Arguments

Returns

string (opens new window)

# 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

string (opens new window)

# dateFunction()

Converts an input to a DateTime (opens new window) instance.

View source (opens new window)

Arguments

Returns

DateTimeInterface (opens new window)

# datetimeFilter()

Formats the value as a date+time.

View source (opens new window)

Arguments

Returns

string (opens new window)

# dumpFunction()

Since
4.4.0

Displays a variable(s).

View source (opens new window)

Arguments

Returns

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

Returns

yii\db\Expression (opens new window)

# filesizeFilter()

Since
3.6.0

Formats the value in bytes as a size in human-readable form, for example 12 kB.

View source (opens new window)

Arguments

Returns

string (opens new window)

# filterFilter()

Filters an array.

View source (opens new window)

Arguments

Returns

array (opens new window)

Throws

  • \Twig\Error\RuntimeError

# getFilters()

View source (opens new window)

# getFunctions()

View source (opens new window)

# getGlobals()

Registers global variables.

View source (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 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

# httpdateFilter()

Since
3.6.10

Converts a date to the HTTP format (used by HTTP headers such as Expires).

View source (opens new window)

Arguments

Returns

string (opens new window) – The formatted date

# 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

string (opens new window), false (opens new window) – 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.

# lengthFilter()

Since
4.2.0

Returns the length of an array, or the total result count of a query.

View source (opens new window)

Arguments

  • $env (\Twig\Environment)
  • $value (mixed) – A variable

Returns

integer (opens new window) – The length of the value

# 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.

# mapFilter()

Since
4.4.16

Maps an array.

View source (opens new window)

Arguments

  • $env (\Twig\Environment)
  • $array (mixed)
  • $arrow (mixed)

Returns

array (opens new window)

Throws

  • \Twig\Error\RuntimeError

# 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.
  • $encode (boolean (opens new window)) – Whether special characters should be pre-encoded, before parsing the text as Markdown. Note that the flavor cannot be specified if this option is used.

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

# moneyFilter()

Outputs a value from a Money object.

View source (opens new window)

Arguments

Returns

string (opens new window), null (opens new window)

# 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), callable (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

# numberFilter()

Since
3.6.0

Formats the value as a decimal number.

View source (opens new window)

Arguments

Returns

string (opens new window)

# 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)

# percentageFilter()

Since
3.6.0

Formats the value as a percent number with "%" sign.

View source (opens new window)

Arguments

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), null (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)

# reduceFilter()

Since
4.4.16

Reduces an array.

View source (opens new window)

Arguments

  • $env (\Twig\Environment)
  • $array (mixed)
  • $arrow (mixed)
  • $initial (mixed)

Returns

mixed

Throws

  • \Twig\Error\RuntimeError

# removeClassFilter()

Since
3.7.0

Removes a class (or classes) from the given HTML tag.

View source (opens new window)

Arguments

Returns

string (opens new window) – The modified HTML tag

# 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

Returns

mixed

# 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 (iterable)

Returns

array (opens new window)

# snakeFilter()

Snake_cases a string.

View source (opens new window)

Arguments

  • $string (mixed) – The string

Returns

string (opens new window)

# sortFilter()

Since
4.3.2

Sorts an array.

View source (opens new window)

Arguments

Returns

array (opens new window)

Throws

  • \Twig\Error\RuntimeError

# 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

string (opens new window)

# timestampFilter()

Since
3.6.0

Formats the value as a human-readable timestamp.

View source (opens new window)

Arguments

Returns

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)

# widontFilter()

Since
3.7.0

Inserts a non-breaking space between the last two words of a string.

View source (opens new window)

Arguments

Returns

string (opens new window)

# withoutFilter()

Returns an array without certain values.

View source (opens new window)

Arguments

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)