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
Protected Properties ​
Property | Description |
---|---|
environment | \Twig\Environment , null |
view | craft\web\View, null |
environment
​
- Type
\Twig\Environment
, null- Default value
null
view
​
- Type
- craft\web\View, null
- Default value
null
Public Methods ​
Method | Description |
---|---|
__construct() | Constructor |
addressFilter() | |
appendFilter() | Appends HTML to the end of the given tag. |
arrayEvery() | |
arraySome() | |
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(). |
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 for the given file path or asset. |
dateFilter() | Extending Twig's |
dateFunction() | Converts an input to a DateTime instance. |
datetimeFilter() | Formats the value as a date+time. |
dumpFunction() | Displays a variable(s). |
encencFilter() | Encrypts and base64-encodes a string. |
entryTypeFunction() | |
expressionFunction() | |
fieldValueSqlFunction() | |
filesizeFilter() | Formats the value in bytes as a size in human-readable form, for example 12 kB . |
filterFilter() | Filters an array. |
findFilter() | |
getFilters() | |
getFunctions() | |
getGlobals() | Registers global variables. |
getNodeVisitors() | |
getOperators() | |
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(). |
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
Arguments ​
$view
(craft\web\View)$environment
(\Twig\Environment
)
addressFilter()
​
- Since
- 4.0.0
Arguments ​
$address
(craft\elements\Address, null)$options
(array)$formatter
(\CommerceGuys\Addressing\Formatter\FormatterInterface
, null)
Returns ​
appendFilter()
​
- Since
- 3.3.0
Appends HTML to the end of the given tag.
Arguments ​
$tag
(string) – The HTML tag that$html
should be appended to$html
(string) – The HTML to append to$tag
.$ifExists
(string, null) – What to do if$tag
already contains a child of the same type as the element defined by$html
. Set to'keep'
if no action should be taken, or'replace'
if it should be replaced by$tag
.
Returns ​
string – The modified HTML
arrayEvery()
​
- Since
- 5.4.3
Arguments ​
$env
$array
$arrow
arraySome()
​
- Since
- 5.4.3
Arguments ​
$env
$array
$arrow
atomFilter()
​
Converts a date to the Atom format.
Arguments ​
$env
(\Twig\Environment
)$date
(DateTime, DateTimeInterface, string) – A date$timezone
(DateTimeZone, string, false, null) – The target timezone, null to use the default, false to leave unchanged
Returns ​
string – The formatted date
attrFilter()
​
- Since
- 3.3.0
Modifies a HTML tag’s attributes, supporting the same attribute definitions as craft\helpers\Html::renderTagAttributes().
Arguments ​
$tag
(string) – The HTML tag whose attributes should be modified.$attributes
(array) – The attributes to be added to the tag.
Returns ​
string – The modified HTML tag.
camelFilter()
​
CamelCases a string.
Arguments ​
$string
(mixed
) – The string
Returns ​
cloneFunction()
​
Returns a clone of the given variable.
Arguments ​
$var
(mixed
)
Returns ​
mixed
collectFunction()
​
- Since
- 4.0.0
Returns a new collection.
Arguments ​
$var
(mixed
)
Returns ​
\Illuminate\Support\Collection
currencyFilter()
​
- Since
- 3.6.0
Formats the value as a currency number.
Arguments ​
Returns ​
dataUrlFunction()
​
- Since
- 3.5.13
Generates a base64-encoded data URL for the given file path or asset.
Arguments ​
$file
(string, craft\elements\Asset) – A file path on an asset$mimeType
(string, null) – The file’s MIME type. Ifnull
then it will be determined automatically.
Returns ​
string – The data URL
Throws ​
- yii\base\InvalidConfigException
if$file
is an invalid file path, or an asset with a missing/invalid volume ID - craft\errors\AssetException
if a stream could not be created for the asset
dateFilter()
​
Extending Twig's |date filter so we can run any translations on the output.
Arguments ​
$env
(\Twig\Environment
)$date
(DateTimeInterface, DateInterval, string) – A date$format
(string, null) – The target format, null to use the default$timezone
(DateTimeZone, string, false, null) – The target timezone, null to use the default, false to leave unchanged$locale
(string, null) – The target locale the date should be formatted for. By default, the current system locale will be used.
Returns ​
dateFunction()
​
Converts an input to a DateTime instance.
Arguments ​
$env
(\Twig\Environment
)$date
(DateTimeInterface, string, array, null) – A date, or null to use the current time$timezone
(DateTimeZone, string, false, null) – The target timezone,null
to use the default,false
to leave unchanged
Returns ​
datetimeFilter()
​
Formats the value as a date+time.
Arguments ​
$env
(\Twig\Environment
)$date
(DateTimeInterface, string) – A date$format
(string, null) – The target format, null to use the default$timezone
(DateTimeZone, string, false, null) – The target timezone, null to use the default, false to leave unchanged$locale
(string, null) – The target locale the date should be formatted for. By default, the current system locale will be used.
Returns ​
dumpFunction()
​
- Since
- 4.4.0
Displays a variable(s).
Arguments ​
$context
(array)$vars
(mixed
)
Returns ​
encencFilter()
​
Encrypts and base64-encodes a string.
Arguments ​
$str
(mixed
) – The string
Returns ​
entryTypeFunction()
​
- Since
- 5.0.0
Arguments ​
$handle
(string)
Returns ​
expressionFunction()
​
- Since
- 3.1.0
Arguments ​
Returns ​
fieldValueSqlFunction()
​
- Since
- 5.0.0
Arguments ​
$provider
(craft\base\FieldLayoutProviderInterface)$fieldHandle
(string)$key
(string, null)
Returns ​
filesizeFilter()
​
- Since
- 3.6.0
Formats the value in bytes as a size in human-readable form, for example 12 kB
.
Arguments ​
Returns ​
filterFilter()
​
Filters an array.
Arguments ​
Returns ​
Throws ​
\Twig\Error\RuntimeError
findFilter()
​
- Since
- 5.4.3
Arguments ​
$env
$array
$arrow
getFilters()
​
getFunctions()
​
getGlobals()
​
Registers global variables.
getNodeVisitors()
​
getOperators()
​
getTests()
​
getTokenParsers()
​
gqlFunction()
​
- Since
- 3.3.12
Executes a GraphQL query against the full schema.
Arguments ​
$query
(string) – The GraphQL query$variables
(array, null) – Query variables$operationName
(string, null) – The operation name
Returns ​
array – The query result
groupFilter()
​
Groups an array by the results of an arrow function, or value of a property.
Arguments ​
$arr
(iterable
)$arrow
(callable, string) – The arrow function or property name that determines the group the item should be grouped in
Returns ​
array[] – 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
).
Arguments ​
$env
(\Twig\Environment
)$date
(DateTime, DateTimeInterface, string) – A date$timezone
(DateTimeZone, string, false, null) – The target timezone, null to use the default, false to leave unchanged
Returns ​
string – The formatted date
indexOfFilter()
​
Returns the index of an item in a string or array, or -1 if it cannot be found.
Arguments ​
$haystack
(mixed
)$needle
(mixed
)
Returns ​
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.
Arguments ​
$value
(mixed
) – The value to JSON encode.$options
(integer, null) – Either null or a bitmask consisting of JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT$depth
(integer) – The maximum depth
Returns ​
string, false – The JSON encoded value.
kebabFilter()
​
Kebab-cases a string.
Arguments ​
$string
(mixed
) – The string$glue
(string) – The string used to glue the words together (default is a hyphen)$lower
(boolean) – Whether the string should be lowercased (default is true)$removePunctuation
(boolean) – Whether punctuation marks should be removed (default is true)
Returns ​
string – The kebab-cased string
lcfirstFilter()
​
Lowercases the first character of a multibyte string.
Arguments ​
$string
(mixed
) – The multibyte string.
Returns ​
string – 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.
Arguments ​
$env
(\Twig\Environment
)$value
(mixed
) – A variable
Returns ​
integer – 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().
Arguments ​
$value
(mixed
) – The param value.
Returns ​
string – The escaped param value.
mapFilter()
​
- Since
- 4.4.16
Maps an array.
Arguments ​
$env
(\Twig\Environment
)$array
(mixed
)$arrow
(mixed
)
Returns ​
Throws ​
\Twig\Error\RuntimeError
markdownFilter()
​
Parses text through Markdown.
Arguments ​
$markdown
(mixed
) – The markdown text to parse$flavor
(string, null) – 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) – Whether to only parse inline elements, omitting any<p>
tags.$encode
(boolean) – Whether special characters should be pre-encoded, before parsing the text as Markdown. Note that theflavor
cannot be specified if this option is used.
Returns ​
mergeFilter()
​
- Since
- 3.4.0
Merges an array with another one.
Arguments ​
$arr1
(iterable
) – An array$arr2
(iterable
) – An array$recursive
(boolean) – Whether the arrays should be merged recursively using yii\helpers\BaseArrayHelper::merge()
Returns ​
array – The merged array
moneyFilter()
​
Outputs a value from a Money object.
Arguments ​
Returns ​
multisortFilter()
​
Duplicates an array and sorts it with craft\helpers\ArrayHelper::multisort().
Arguments ​
$array
(mixed
) – The array to be sorted. The array will be modified after calling this method.$key
(string, callable, array) – 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, array) – The sorting direction. It can be eitherSORT_ASC
orSORT_DESC
. When sorting by multiple keys with different sorting directions, use an array of sorting directions.$sortFlag
(integer, array) – The PHP sort flag. Valid values includeSORT_REGULAR
,SORT_NUMERIC
,SORT_STRING
,SORT_LOCALE_STRING
,SORT_NATURAL
andSORT_FLAG_CASE
. Please refer to PHP manual for more details. When sorting by multiple keys with different sort flags, use an array of sort flags.
Returns ​
array – The sorted array
Throws ​
- yii\base\InvalidArgumentException
if the $direction or $sortFlag parameters do not have correct number of elements as that of $key.
numberFilter()
​
- Since
- 3.6.0
Formats the value as a decimal number.
Arguments ​
Returns ​
parseAttrFilter()
​
- Since
- 3.4.0
Parses an HTML tag to find its attributes.
Arguments ​
$tag
(string) – The HTML tag to parse
Returns ​
array – The parsed HTML tag attributes
Throws ​
- yii\base\InvalidArgumentException
if$tag
doesn't contain a valid HTML tag
parseRefsFilter()
​
Parses a string for reference tags.
Arguments ​
Returns ​
pascalFilter()
​
PascalCases a string.
Arguments ​
$string
(mixed
) – The string
Returns ​
percentageFilter()
​
- Since
- 3.6.0
Formats the value as a percent number with "%" sign.
Arguments ​
Returns ​
pluginFunction()
​
- Since
- 3.1.0
Returns a plugin instance by its handle.
Arguments ​
$handle
(string) – The plugin handle
Returns ​
craft\base\PluginInterface, null – The plugin, or null
if it’s not installed
prependFilter()
​
- Since
- 3.3.0
Prepends HTML to the beginning of given tag.
Arguments ​
$tag
(string) – The HTML tag that$html
should be prepended to$html
(string) – The HTML to prepend to$tag
.$ifExists
(string, null) – What to do if$tag
already contains a child of the same type as the element defined by$html
. Set to'keep'
if no action should be taken, or'replace'
if it should be replaced by$tag
.
Returns ​
string – The modified HTML
purifyFilter()
​
- Since
- 3.4.0
Purifies the given HTML using HTML Purifier.
Arguments ​
$html
(string, null) – The HTML to be purified$config
(string, array, null) – The HTML Purifier config. This can either be the name of a JSON file withinconfig/htmlpurifier/
(sans.json
extension) or a config array.
Returns ​
string, null – The purified HTML
pushFilter()
​
- Since
- 3.5.0
Pushes one or more items onto the end of an array.
Arguments ​
$array
(array)
Returns ​
reduceFilter()
​
- Since
- 4.4.16
Reduces an array.
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.
Arguments ​
Returns ​
string – The modified HTML tag
renderObjectTemplate()
​
Arguments ​
$template
(string)$object
(mixed
)
Returns ​
replaceFilter()
​
Replaces Twig's |replace filter, adding support for passing in separate search and replace arrays.
Arguments ​
Returns ​
mixed
rssFilter()
​
Converts a date to the RSS format.
Arguments ​
$env
(\Twig\Environment
)$date
(DateTime, DateTimeInterface, string) – A date$timezone
(DateTimeZone, string, false, null) – The target timezone, null to use the default, false to leave unchanged
Returns ​
string – The formatted date
seqFunction()
​
- Since
- 3.0.31
Returns the next number in a given sequence, or the current number in the sequence.
Arguments ​
$name
(string) – The sequence name.$length
(integer, null) – The minimum string length that should be returned. (Numbers that are too short will be left-padded with0
s.)$next
(boolean) – Whether the next number in the sequence should be returned (and the sequence should be incremented). If set tofalse
, the current number in the sequence will be returned instead.
Returns ​
Throws ​
- Throwable
if reasons - yii\db\Exception
shuffleFunction()
​
Shuffles an array.
Arguments ​
$arr
(iterable
)
Returns ​
snakeFilter()
​
Snake_cases a string.
Arguments ​
$string
(mixed
) – The string
Returns ​
sortFilter()
​
- Since
- 4.3.2
Sorts an array.
Arguments ​
Returns ​
Throws ​
\Twig\Error\RuntimeError
svgFunction()
​
Returns the contents of a given SVG file.
Arguments ​
$svg
(string, craft\elements\Asset) – An SVG asset, a file path, or raw SVG markup$sanitize
(boolean, null) – Whether the SVG should be sanitized of potentially malicious scripts. By default, the SVG will only be sanitized if an asset or markup is passed in. (File paths are assumed to be safe.)$namespace
(boolean, null) – Whether class names and IDs within the SVG should be namespaced to avoid conflicts with other elements in the DOM. By default, the SVG will only be namespaced if an asset or markup is passed in.$class
(string, null) – A CSS class name that should be added to the<svg>
element. (This argument is deprecated. The|attr
filter should be used instead.)
Returns ​
tagFunction()
​
- Since
- 3.3.0
Generates a complete HTML tag.
Arguments ​
$type
(string) – The tag type ('p', 'div', etc.)$attributes
(array) – The HTML tag attributes in terms of name-value pairs. Iftext
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 ​
timeFilter()
​
Formats the value as a time.
Arguments ​
$env
(\Twig\Environment
)$date
(DateTimeInterface, string) – A date$format
(string, null) – The target format, null to use the default$timezone
(DateTimeZone, string, false, null) – The target timezone, null to use the default, false to leave unchanged$locale
(string, null) – The target locale the date should be formatted for. By default, the current system locale will be used.
Returns ​
timestampFilter()
​
- Since
- 3.6.0
Formats the value as a human-readable timestamp.
Arguments ​
Returns ​
translateFilter()
​
Translates the given message.
Arguments ​
$message
(mixed
) – The message to be translated.$category
(string, array, null) – The message category.$params
(array, string, null) – The parameters that will be used to replace the corresponding placeholders in the message.$language
(string, null) – The language code (e.g.en-US
,en
). If this is null, the current application language will be used.
Returns ​
string – The translated message.
truncateFilter()
​
- Since
- 3.5.10
Truncates the string to a given length, while ensuring that it does not split words.
Arguments ​
$string
(string) – The string to truncate$length
(integer) – The maximum number of characters for the truncated string$suffix
(string) – The string that should be appended to$string
, if it must be truncated$splitSingleWord
(boolean) – Whether to split up$string
if it only contains one word
Returns ​
string – The truncated string
ucfirstFilter()
​
Uppercases the first character of a multibyte string.
Arguments ​
$string
(mixed
) – The multibyte string.
Returns ​
string – The string with the first character converted to upercase.
ucwordsFilter()
​
Uppercases the first character of each word in a string.
Arguments ​
$env
(\Twig\Environment
)$string
(string)
Returns ​
unshiftFilter()
​
- Since
- 3.5.0
Prepends one or more items to the beginning of an array.
Arguments ​
$array
(array)
Returns ​
widontFilter()
​
- Since
- 3.7.0
Inserts a non-breaking space between the last two words of a string.
Arguments ​
$string
(string)
Returns ​
withoutFilter()
​
Returns an array without certain values.
Arguments ​
$arr
(mixed
)$exclude
(mixed
)$strict
(boolean)
Returns ​
withoutKeyFilter()
​
- Since
- 3.2.0
Returns an array without a certain key.