Extension

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

Class Extension

View source

Protected Properties

PropertyDescription
environment\Twig\Environment, null
viewcraft\web\View, null

environment

Type
\Twig\Environment, null

View source

view

Type
craft\web\View, null

View source

Public Methods

MethodDescription
__construct()Constructor
actionInputFunction()Returns an action input wrapped in a \Twig\Markup object, suitable for use in a front-end form.
atomFilter()Converts a date to the Atom format.
attrFunction()Renders HTML tag attributes with craft\helpers\Html::renderTagAttributes()
camelFilter()CamelCases a string.
cloneFunction()Returns a clone of the given variable.
csrfInputFunction()Returns a CSRF input wrapped in a \Twig\Markup object.
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()
groupFilter()Groups an array or element query's results by a common 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.
multisortFilter()Duplicates an array and sorts it with craft\helpers\ArrayHelper::multisort().
parseRefsFilter()Parses a string for reference tags.
pascalFilter()PascalCases a string.
pluginFunction()Returns a plugin instance by its handle.
redirectInputFunction()Returns a redirect input wrapped in a \Twig\Markup object.
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.
timeFilter()Formats the value as a time.
translateFilter()Translates the given message.
ucfirstFilter()Uppercases the first character of a multibyte string.
withoutFilter()Returns an array without certain values.
withoutKeyFilter()Returns an array without a certain key.

__construct()

Constructor

View source

Arguments

actionInputFunction()

Returns an action input wrapped in a \Twig\Markup object, suitable for use in a front-end form.

View source

Arguments

Returns

\Twig\Markup

atomFilter()

Converts a date to the Atom format.

View source

Arguments

Returns

string – The formatted date

attrFunction()

Renders HTML tag attributes with craft\helpers\Html::renderTagAttributes()

View source

Arguments

  • $config

Returns

\Twig\Markup

camelFilter()

CamelCases a string.

View source

Arguments

  • $string (mixed) – The string

Returns

string

cloneFunction()

Returns a clone of the given variable.

View source

Arguments

  • $var (mixed)

Returns

mixed

csrfInputFunction()

Returns a CSRF input wrapped in a \Twig\Markup object.

View source

Returns

\Twig\Markup, null

dateFilter()

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

View source

Arguments

Returns

mixed, string

datetimeFilter()

Formats the value as a date+time.

View source

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 systme locale will be used.

Returns

mixed, string

encencFilter()

Encrypts and base64-encodes a string.

View source

Arguments

  • $str (mixed) – The string

Returns

string

expressionFunction()

View source

Arguments

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

Returns

yii\db\Expression

filterFilter()

Filters an array.

View source

Arguments

Returns

array

getCsrfInput()

DEPRECATED

Deprecated in Craft 3.0. Use csrfInput() instead.

View source

Returns

\Twig\Markup, null

getFilters()

View source

getFootHtml()

DEPRECATED

Deprecated in Craft 3.0. Use endBody() instead.

View source

Returns

\Twig\Markup

getFunctions()

View source

getGlobals()

View source

getHeadHtml()

DEPRECATED

Deprecated in Craft 3.0. Use head() instead.

View source

Returns

\Twig\Markup

getNodeVisitors()

View source

getTests()

View source

getTokenParsers()

View source

groupFilter()

Groups an array or element query's results by a common property.

View source

Arguments

Returns

array

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

Arguments

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

Returns

integer

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

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

mixed – The JSON encoded value.

kebabFilter()

Kebab-cases a string.

View source

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.

View source

Arguments

  • $string (mixed) – The multibyte string.

Returns

string – 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

Arguments

  • $value (mixed) – The param value.

Returns

string – The escaped param value.

markdownFilter()

Parses text through Markdown.

View source

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.

Returns

\Twig\Markup

multisortFilter()

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

View source

Arguments

  • $array (mixed) – The array to be sorted. The array will be modified after calling this method.
  • $key (string, Closure, 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 either SORT_ASC or SORT_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 include SORT_REGULAR, SORT_NUMERIC, SORT_STRING, SORT_LOCALE_STRING, SORT_NATURAL and SORT_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

parseRefsFilter()

Parses a string for reference tags.

View source

Arguments

Returns

\Twig\Markup

pascalFilter()

PascalCases a string.

View source

Arguments

  • $string (mixed) – The string

Returns

string

pluginFunction()

Returns a plugin instance by its handle.

View source

Arguments

  • $handle (string) – The plugin handle

Returns

craft\base\PluginInterface, null – The plugin, or null if it's not installed

redirectInputFunction()

Returns a redirect input wrapped in a \Twig\Markup object.

View source

Arguments

  • $url (string) – The URL to redirect to.

Returns

\Twig\Markup

renderObjectTemplate()

View source

Arguments

  • $template (string)
  • $object (mixed)

Returns

string

replaceFilter()

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

View source

Arguments

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

Returns

mixed

roundFunction()

DEPRECATED

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

Rounds the given value.

View source

Arguments

Returns

integer, float

rssFilter()

Converts a date to the RSS format.

View source

Arguments

Returns

string – The formatted date

seqFunction()

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

View source

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 with 0s.)
  • $next (boolean) – Whether the next number in the sequence should be returned (and the sequence should be incremented). If set to false, the current number in the sequence will be returned instead.

Returns

integer, string

Throws

shuffleFunction()

Shuffles an array.

View source

Arguments

  • $arr (mixed)

Returns

mixed

snakeFilter()

Snake_cases a string.

View source

Arguments

  • $string (mixed) – The string

Returns

string

svgFunction()

Returns the contents of a given SVG file.

View source

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.

Returns

\Twig\Markup, string

timeFilter()

Formats the value as a time.

View source

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 systme locale will be used.

Returns

mixed, string

translateFilter()

Translates the given message.

View source

Arguments

  • $message (mixed) – The message to be translated.
  • $category (string, null) – The message category.
  • $params (array, 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.

ucfirstFilter()

Uppercases the first character of a multibyte string.

View source

Arguments

  • $string (mixed) – The multibyte string.

Returns

string – The string with the first character converted to upercase.

withoutFilter()

Returns an array without certain values.

View source

Arguments

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

Returns

array

withoutKeyFilter()

Returns an array without a certain key.

View source

Arguments

Returns

array