Craft 3 Class Reference

Class craft\web\twig\Extension

Inheritance
craft\web\twig\Extension ยป Twig_Extension
Implements
Twig_ExtensionInterface, Twig_Extension_GlobalsInterface
Available since version
3.0
Source Code
https://github.com/craftcms/cms/blob/master/src/web/twig/Extension.php

Class Extension

Protected Properties
Property Type Description Defined By
$environment craft\web\twig\Environment, null craft\web\twig\Extension
$view craft\web\View, null craft\web\twig\Extension
Public Methods
Method Description Defined By
__construct() Constructor craft\web\twig\Extension
atomFilter() Converts a date to the Atom format. craft\web\twig\Extension
camelFilter() CamelCases a string. craft\web\twig\Extension
cloneFunction() Returns a clone of the given variable. craft\web\twig\Extension
csrfInputFunction() Returns a CSRF input wrapped in a \Twig_Markup object. craft\web\twig\Extension
dateFilter() Extending Twig's |date filter so we can run any translations on the output. craft\web\twig\Extension
datetimeFilter() Formats the value as a date+time. craft\web\twig\Extension
encencFilter() Encrypts and base64-encodes a string. craft\web\twig\Extension
getCsrfInput() craft\web\twig\Extension
getFilters() Returns a list of filters to add to the existing list. craft\web\twig\Extension
getFootHtml() craft\web\twig\Extension
getFunctions() Returns a list of functions to add to the existing list. craft\web\twig\Extension
getGlobals() Returns a list of global variables to add to the existing list. craft\web\twig\Extension
getHeadHtml() craft\web\twig\Extension
getName() Returns the name of the extension. craft\web\twig\Extension
getNodeVisitors() craft\web\twig\Extension
getOperators() Twig_Extension
getTests() {@inheritdoc} craft\web\twig\Extension
getTokenParsers() Returns the token parser instances to add to the existing list. craft\web\twig\Extension
groupFilter() Groups an array or element query's results by a common property. craft\web\twig\Extension
indexOfFilter() Returns the index of an item in a string or array, or -1 if it cannot be found. craft\web\twig\Extension
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. craft\web\twig\Extension
kebabFilter() Kebab-cases a string. craft\web\twig\Extension
lcfirstFilter() Lowercases the first character of a multibyte string. craft\web\twig\Extension
literalFilter() Escapes commas and asterisks in a string so they are not treated as special characters in craft\helpers\Db::parseParam(). craft\web\twig\Extension
markdownFilter() Parses text through Markdown. craft\web\twig\Extension
multisortFilter() Duplicates an array and sorts it with craft\helpers\ArrayHelper::multisort(). craft\web\twig\Extension
parseRefsFilter() Parses a string for reference tags. craft\web\twig\Extension
pascalFilter() PascalCases a string. craft\web\twig\Extension
redirectInputFunction() Returns a redirect input wrapped in a \Twig_Markup object. craft\web\twig\Extension
renderObjectTemplate() craft\web\twig\Extension
replaceFilter() Replaces Twig's |replace filter, adding support for passing in separate search and replace arrays. craft\web\twig\Extension
roundFunction() Rounds the given value. craft\web\twig\Extension
rssFilter() Converts a date to the RSS format. craft\web\twig\Extension
shuffleFunction() Shuffles an array. craft\web\twig\Extension
snakeFilter() Snake_cases a string. craft\web\twig\Extension
svgFunction() Returns the (sanitized) contents of a given SVG file, namespacing any of its IDs in the process. craft\web\twig\Extension
timeFilter() Formats the value as a time. craft\web\twig\Extension
translateFilter() Translates the given message. craft\web\twig\Extension
ucfirstFilter() Uppercases the first character of a multibyte string. craft\web\twig\Extension
withoutFilter() Returns an array without certain values. craft\web\twig\Extension

Property Details

$environment protected property #

protected craft\web\twig\Environment, null $environment null

$view protected property #

protected craft\web\View, null $view null

Method Details

__construct() public method #

Constructor

public void __construct ( craft\web\View $view, craft\web\twig\Environment $environment )
$view craft\web\View
$environment craft\web\twig\Environment

atomFilter() public method #

Converts a date to the Atom format.

public string atomFilter ( Twig_Environment $env, $date, $timezone null )
$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
return string The formatted date

camelFilter() public method #

CamelCases a string.

public string camelFilter ( \craft\web\twig\string $string )
$string string The string

cloneFunction() public method #

Returns a clone of the given variable.

public mixed cloneFunction ( $var )
$var mixed

csrfInputFunction() public method #

Returns a CSRF input wrapped in a \Twig_Markup object.

public Twig_Markup, null csrfInputFunction ( )

dateFilter() public method #

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

public mixed, string dateFilter ( Twig_Environment $env, $date, \craft\web\twig\string $format null, $timezone null, \craft\web\twig\bool $translate true )
$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
$translate boolean Whether the formatted date string should be translated

datetimeFilter() public method #

Formats the value as a date+time.

public mixed, string datetimeFilter ( Twig_Environment $env, $date, \craft\web\twig\string $format null, $timezone null, \craft\web\twig\bool $translate true )
$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
$translate boolean Whether the formatted date string should be translated

encencFilter() public method #

Encrypts and base64-encodes a string.

public string encencFilter ( \craft\web\twig\string $str )
$str string The string

getCsrfInput() public method #

Deprecated in Craft 3.0. Use csrfInput() instead.

public Twig_Markup, null getCsrfInput ( )

getFilters() public method #

Returns a list of filters to add to the existing list.

public Twig_SimpleFilter getFilters ( )
return Twig_SimpleFilter An array of filters

getFootHtml() public method #

Deprecated in Craft 3.0. Use endBody() instead.

public Twig_Markup getFootHtml ( )

getFunctions() public method #

Returns a list of functions to add to the existing list.

public Twig_SimpleFunction getFunctions ( )
return Twig_SimpleFunction An array of functions

getGlobals() public method #

Returns a list of global variables to add to the existing list.

public array getGlobals ( )
return array An array of global variables

getHeadHtml() public method #

Deprecated in Craft 3.0. Use head() instead.

public Twig_Markup getHeadHtml ( )

getName() public method #

Returns the name of the extension.

public string getName ( )
return string The extension name

getNodeVisitors() public method #

public void getNodeVisitors ( )

getTests() public method #

{@inheritdoc}

public void getTests ( )

getTokenParsers() public method #

Returns the token parser instances to add to the existing list.

public array getTokenParsers ( )
return array An array of Twig_TokenParserInterface or Twig_TokenParserBrokerInterface instances

groupFilter() public method #

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

public array groupFilter ( $arr, \craft\web\twig\string $item )
$arr array, Traversable
$item string
throws Twig_Error_Runtime if $arr is not of type array or Traversable

indexOfFilter() public method #

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

public integer indexOfFilter ( $haystack, $needle )
$haystack mixed
$needle mixed

jsonEncodeFilter() public method #

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.

public mixed jsonEncodeFilter ( $value, \craft\web\twig\int $options null, \craft\web\twig\int $depth 512 )
$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
return mixed The JSON encoded value.

kebabFilter() public method #

Kebab-cases a string.

public string kebabFilter ( \craft\web\twig\string $string, \craft\web\twig\string $glue '-', \craft\web\twig\bool $lower true, \craft\web\twig\bool $removePunctuation true )
$string string 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)
return string The kebab-cased string

lcfirstFilter() public method #

Lowercases the first character of a multibyte string.

public string lcfirstFilter ( \craft\web\twig\string $string )
$string string The multibyte string.
return string The string with the first character converted to lowercase.

literalFilter() public method #

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

public string literalFilter ( \craft\web\twig\string $value )
$value string The param value.
return string The escaped param value.

markdownFilter() public method #

Parses text through Markdown.

public Twig_Markup markdownFilter ( \craft\web\twig\string $markdown, \craft\web\twig\string $flavor null, \craft\web\twig\bool $inlineOnly false )
$markdown string 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.

multisortFilter() public method #

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

public array multisortFilter ( array $array, $key, $direction SORT_ASC, $sortFlag SORT_REGULAR )
$array array 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.
return 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.

parseRefsFilter() public method #

Parses a string for reference tags.

public Twig_Markup parseRefsFilter ( \craft\web\twig\string $str, \craft\web\twig\int $siteId null )
$str string
$siteId integer, null

pascalFilter() public method #

PascalCases a string.

public string pascalFilter ( \craft\web\twig\string $string )
$string string The string

redirectInputFunction() public method #

Returns a redirect input wrapped in a \Twig_Markup object.

public Twig_Markup redirectInputFunction ( \craft\web\twig\string $url )
$url string The URL to redirect to.

renderObjectTemplate() public method #

public string renderObjectTemplate ( \craft\web\twig\string $template, $object )
$template string
$object mixed

replaceFilter() public method #

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

public mixed replaceFilter ( $str, $search, $replace null )
$str mixed
$search mixed
$replace mixed

roundFunction() public method #

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

Rounds the given value.

public integer, float roundFunction ( $value, \craft\web\twig\int $precision 0, \craft\web\twig\int $mode PHP_ROUND_HALF_UP )
$value integer, float
$precision integer
$mode integer

rssFilter() public method #

Converts a date to the RSS format.

public string rssFilter ( Twig_Environment $env, $date, $timezone null )
$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
return string The formatted date

shuffleFunction() public method #

Shuffles an array.

public mixed shuffleFunction ( $arr )
$arr mixed

snakeFilter() public method #

Snake_cases a string.

public string snakeFilter ( \craft\web\twig\string $string )
$string string The string

svgFunction() public method #

Returns the (sanitized) contents of a given SVG file, namespacing any of its IDs in the process.

public Twig_Markup, string svgFunction ( \craft\web\twig\string $svg, \craft\web\twig\bool $sanitize true )
$svg string The SVG file path or contents
$sanitize boolean Whether the file should be sanitized first

timeFilter() public method #

Formats the value as a time.

public mixed, string timeFilter ( Twig_Environment $env, $date, \craft\web\twig\string $format null, $timezone null, \craft\web\twig\bool $translate true )
$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
$translate boolean Whether the formatted date string should be translated

translateFilter() public method #

Translates the given message.

public string translateFilter ( $message, $category null, $params null, $language null )
$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.
return string The translated message.

ucfirstFilter() public method #

Uppercases the first character of a multibyte string.

public string ucfirstFilter ( \craft\web\twig\string $string )
$string string The multibyte string.
return string The string with the first character converted to upercase.

withoutFilter() public method #

Returns an array without certain values.

public array withoutFilter ( array $arr, $exclude )
$arr array
$exclude mixed