\PicoTwigExtension

Pico's Twig extension to implement additional filters

Summary

Methods
Properties
Constants
__construct()
getPico()
getName()
getFilters()
getFunctions()
markdownFilter()
mapFilter()
sortByFilter()
getKeyOfVar()
urlParamFunction()
formParamFunction()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
No private methods found
$pico
N/A

Properties

$pico

$pico : \Pico

Current instance of Pico

Type

\Pico

Methods

__construct()

__construct(\Pico  $pico) 

Constructs a new instance of this Twig extension

Parameters

\Pico $pico

current instance of Pico

getPico()

getPico() : \Pico

Returns the extensions instance of Pico

Returns

\Pico

the extension's instance of Pico

getName()

getName() : string

Returns the name of the extension

Returns

string —

the extension name

getFilters()

getFilters() : array<mixed,\Twig_SimpleFilter>

Returns a list of Pico-specific Twig filters

Returns

array<mixed,\Twig_SimpleFilter> —

array of Pico's Twig filters

getFunctions()

getFunctions() : array<mixed,\Twig_SimpleFunction>

Returns a list of Pico-specific Twig functions

Returns

array<mixed,\Twig_SimpleFunction> —

array of Pico's Twig functions

markdownFilter()

markdownFilter(string  $markdown, array  $meta = array()) : string

Parses a markdown string to HTML

This method is registered as the Twig markdown filter. You can use it to e.g. parse a meta variable ({{ meta.description|markdown }}). Don't use it to parse the contents of a page, use the content filter instead, what ensures the proper preparation of the contents.

Parameters

string $markdown

markdown to parse

array $meta

meta data to use for %meta.*% replacement

Returns

string —

parsed HTML

mapFilter()

mapFilter(array|\Traversable  $var, mixed  $mapKeyPath) : array

Returns a array with the values of the given key or key path

This method is registered as the Twig map filter. You can use this filter to e.g. get all page titles ({{ pages|map("title") }}).

Parameters

array|\Traversable $var

variable to map

mixed $mapKeyPath

key to map; either a scalar or a array interpreted as key path (i.e. ['foo', 'bar'] will return all $item['foo']['bar'] values)

Returns

array —

mapped values

sortByFilter()

sortByFilter(array|\Traversable  $var, mixed  $sortKeyPath, string  $fallback = 'bottom') : array

Sorts an array by one of its keys or a arbitrary deep sub-key

This method is registered as the Twig sort_by filter. You can use this filter to e.g. sort the pages array by a arbitrary meta value. Calling {{ pages|sort_by([ "meta", "nav" ]) }} returns all pages sorted by the meta value nav. The sorting algorithm will never assume equality of two values, it will then fall back to the original order. The result is always sorted in ascending order, apply Twigs reverse filter to achieve a descending order.

Parameters

array|\Traversable $var

variable to sort

mixed $sortKeyPath

key to use for sorting; either a scalar or a array interpreted as key path (i.e. ['foo', 'bar'] will sort $var by $item['foo']['bar'])

string $fallback

specify what to do with items which don't contain the specified sort key; use "bottom" (default) to move these items to the end of the sorted array, "top" to rank them first, "keep" to keep the original order, or "remove" to remove these items

Returns

array —

sorted array

getKeyOfVar()

getKeyOfVar(array|\Traversable|\ArrayAccess|object  $var, mixed  $keyPath) : mixed

Returns the value of a variable item specified by a scalar key or a arbitrary deep sub-key using a key path

Parameters

array|\Traversable|\ArrayAccess|object $var

base variable

mixed $keyPath

scalar key or a array interpreted as key path (when passing e.g. ['foo', 'bar'], the method will return $var['foo']['bar']) specifying the value

Returns

mixed —

the requested value or NULL when the given key or key path didn't match

urlParamFunction()

urlParamFunction(string  $name, integer|string  $filter = '', mixed|array  $options = null, integer|string|array<mixed,integer>|array<mixed,string>  $flags = null) : mixed

Filters a URL GET parameter with a specified filter

The Twig function disallows the use of the callback filter.

Parameters

string $name

name of the URL GET parameter to filter

integer|string $filter

the filter to apply

mixed|array $options

either a associative options array to be used by the filter or a scalar default value

integer|string|array<mixed,integer>|array<mixed,string> $flags

flags and flag strings to be used by the filter

Returns

mixed —

either the filtered data, FALSE if the filter fails, or NULL if the URL GET parameter doesn't exist and no default value is given

formParamFunction()

formParamFunction(string  $name, integer|string  $filter = '', mixed|array  $options = null, integer|string|array<mixed,integer>|array<mixed,string>  $flags = null) : mixed

Filters a HTTP POST parameter with a specified filter

The Twig function disallows the use of the callback filter.

Parameters

string $name

name of the HTTP POST parameter to filter

integer|string $filter

the filter to apply

mixed|array $options

either a associative options array to be used by the filter or a scalar default value

integer|string|array<mixed,integer>|array<mixed,string> $flags

flags and flag strings to be used by the filter

Returns

mixed —

either the filtered data, FALSE if the filter fails, or NULL if the HTTP POST parameter doesn't exist and no default value is given