Extension

Type
Class
Namespace
craft\web\twig
Inherits
craft\web\twig\Extension » Twig_Extension
Implements
Twig_ExtensionInterface, Twig_Extension_GlobalsInterface
Since
3.0

Class Extension

View source

Protected Properties

$environment

Signature

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

$view

Signature

protected craft\web\View, null $view = null

Public Methods

MethodDescription
__construct()Constructor
atomFilter()Converts a date to the Atom format.
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.
getCsrfInput()
getFilters()Returns a list of filters to add to the existing list.
getFootHtml()
getFunctions()Returns a list of functions to add to the existing list.
getGlobals()Returns a list of global variables to add to the existing list.
getHeadHtml()
getName()Returns the name of the extension.
getNodeVisitors()
getOperators()
getTests()
getTokenParsers()Returns the token parser instances to add to the existing list.
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.
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.
shuffleFunction()Shuffles an array.
snakeFilter()Snake_cases a string.
svgFunction()Returns the (sanitized) contents of a given SVG file, namespacing any of its IDs in the process.
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.

__construct()

Constructor

View source

Arguments

Signature

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

atomFilter()

Converts a date to the Atom format.

View source

Arguments

Returns

string – The formatted date

Signature

public string atomFilter ( Twig_Environment $env, $date, $timezone = null )

camelFilter()

CamelCases a string.

View source

Arguments

  • $string (string) – The string

Returns

string

Signature

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

cloneFunction()

Returns a clone of the given variable.

View source

Arguments

  • $var (mixed)

Returns

mixed

Signature

public mixed cloneFunction ( $var )

csrfInputFunction()

Returns a CSRF input wrapped in a \Twig_Markup object.

View source

Returns

Twig_Markup, null

Signature

public Twig_Markup, null csrfInputFunction ( )

dateFilter()

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

View source

Arguments

Returns

mixed, string

Signature

public mixed, string dateFilter ( Twig_Environment $env, $date, \craft\web\twig\string $format = null, $timezone = null, \craft\web\twig\string $locale = null )

datetimeFilter()

Formats the value as a date+time.

View source

Arguments

Returns

mixed, string

Signature

public mixed, string datetimeFilter ( Twig_Environment $env, $date, \craft\web\twig\string $format = null, $timezone = null, \craft\web\twig\string $locale = null )

encencFilter()

Encrypts and base64-encodes a string.

View source

Arguments

Returns

string

Signature

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

getCsrfInput()

DEPRECATED

Deprecated in Craft 3.0. Use csrfInput() instead.

View source

Returns

Twig_Markup, null

Signature

public Twig_Markup, null getCsrfInput ( )

getFilters()

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

View source

Returns

Twig_SimpleFilter – An array of filters

Signature

public Twig_SimpleFilter getFilters ( )

getFootHtml()

DEPRECATED

Deprecated in Craft 3.0. Use endBody() instead.

View source

Returns

Twig_Markup

Signature

public Twig_Markup getFootHtml ( )

getFunctions()

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

View source

Returns

Twig_SimpleFunction – An array of functions

Signature

public Twig_SimpleFunction getFunctions ( )

getGlobals()

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

View source

Returns

array – An array of global variables

Signature

public array getGlobals ( )

getHeadHtml()

DEPRECATED

Deprecated in Craft 3.0. Use head() instead.

View source

Returns

Twig_Markup

Signature

public Twig_Markup getHeadHtml ( )

getName()

Returns the name of the extension.

View source

Returns

string – The extension name

Signature

public string getName ( )

getNodeVisitors()

Signature

public void getNodeVisitors ( )

getTests()

Signature

public void getTests ( )

getTokenParsers()

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

View source

Returns

array – An array of Twig_TokenParserInterface or Twig_TokenParserBrokerInterface instances

Signature

public array getTokenParsers ( )

groupFilter()

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

View source

Arguments

Returns

array

Throws

Signature

public array groupFilter ( $arr, \craft\web\twig\string $item )

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

Signature

public integer indexOfFilter ( $haystack, $needle )

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.

Signature

public mixed jsonEncodeFilter ( $value, \craft\web\twig\int $options = null, \craft\web\twig\int $depth = 512 )

kebabFilter()

Kebab-cases a string.

View source

Arguments

  • $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)

Returns

string – The kebab-cased string

Signature

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 )

lcfirstFilter()

Lowercases the first character of a multibyte string.

View source

Arguments

  • $string (string) – The multibyte string.

Returns

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

Signature

public string lcfirstFilter ( \craft\web\twig\string $string )

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 (string) – The param value.

Returns

string – The escaped param value.

Signature

public string literalFilter ( \craft\web\twig\string $value )

markdownFilter()

Parses text through Markdown.

View source

Arguments

  • $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.

Returns

Twig_Markup

Signature

public Twig_Markup markdownFilter ( \craft\web\twig\string $markdown, \craft\web\twig\string $flavor = null, \craft\web\twig\bool $inlineOnly = false )

multisortFilter()

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

View source

Arguments

  • $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.

Returns

array – The sorted array

Throws

Signature

public array multisortFilter ( array $array, $key, $direction = SORT_ASC, $sortFlag = SORT_REGULAR )

parseRefsFilter()

Parses a string for reference tags.

View source

Arguments

Returns

Twig_Markup

Signature

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

pascalFilter()

PascalCases a string.

View source

Arguments

  • $string (string) – The string

Returns

string

Signature

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

redirectInputFunction()

Returns a redirect input wrapped in a \Twig_Markup object.

View source

Arguments

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

Returns

Twig_Markup

Signature

public Twig_Markup redirectInputFunction ( \craft\web\twig\string $url )

renderObjectTemplate()

View source

Arguments

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

Returns

string

Signature

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

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

Signature

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

roundFunction()

DEPRECATED

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

Rounds the given value.

View source

Arguments

Returns

integer, float

Signature

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

rssFilter()

Converts a date to the RSS format.

View source

Arguments

Returns

string – The formatted date

Signature

public string rssFilter ( Twig_Environment $env, $date, $timezone = null )

shuffleFunction()

Shuffles an array.

View source

Arguments

  • $arr (mixed)

Returns

mixed

Signature

public mixed shuffleFunction ( $arr )

snakeFilter()

Snake_cases a string.

View source

Arguments

  • $string (string) – The string

Returns

string

Signature

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

svgFunction()

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

View source

Arguments

Returns

Twig_Markup, string

Signature

public Twig_Markup, string svgFunction ( $svg, \craft\web\twig\bool $sanitize = true )

timeFilter()

Formats the value as a time.

View source

Arguments

Returns

mixed, string

Signature

public mixed, string timeFilter ( Twig_Environment $env, $date, \craft\web\twig\string $format = null, $timezone = null, \craft\web\twig\string $locale = null )

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.

Signature

public string translateFilter ( $message, $category = null, $params = null, $language = null )

ucfirstFilter()

Uppercases the first character of a multibyte string.

View source

Arguments

  • $string (string) – The multibyte string.

Returns

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

Signature

public string ucfirstFilter ( \craft\web\twig\string $string )

withoutFilter()

Returns an array without certain values.

View source

Arguments

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

Returns

array

Signature

public array withoutFilter ( array $arr, $exclude )