Core extends AbstractExtension
Core Twig extension.
This extension provides various utility functions and filters for use in Twig templates, including URL generation, asset management, content processing, and more.
Table of Contents
Properties
Methods
- __construct() : mixed
- asset() : Asset
- Creates an Asset (CSS, JS, images, etc.) from a path or an array of paths.
- avif() : Asset
- Converts an image Asset to AVIF format.
- cacheKey() : string
- Builds a cache key from a variable.
- dataurl() : string
- Returns the data URL of an image.
- dominantColor() : string
- Returns the dominant hex color of an image asset.
- fingerprint() : Asset
- Fingerprinting an asset.
- getEnv() : string|null
- Gets the value of an environment variable.
- getFilters() : array<string|int, mixed>
- getFunctions() : mixed
- getTests() : mixed
- hash() : string
- Hashing an object, an array or a string (with algo, xxh128 by default).
- html() : string
- Creates the HTML element of an asset.
- htmlAudio() : string
- Builds the HTML audio element of an audio Asset.
- htmlCss() : string
- Builds the HTML link element of a CSS Asset.
- htmlImage() : string
- Builds the HTML img element of an image Asset.
- htmlImageFromWebsite() : string|null
- Builds the HTML img element from a website URL by extracting the image from meta tags.
- htmlJs() : string
- Builds the HTML script element of a JS Asset.
- htmlVideo() : string
- Builds the HTML video element of a video Asset.
- imageSizes() : string
- Returns the HTML img `sizes` attribute based on a CSS class name.
- imageSrcset() : string
- Builds the HTML img `srcset` (responsive) attribute of an image Asset, based on configured widths.
- inline() : string
- Returns the content of an asset.
- integrity() : string
- Hashing an asset with algo (sha384 by default).
- isAsset() : bool
- Tests if a variable is an Asset.
- isImageLarge() : bool
- Tests if an image Asset is large enough to be used as a cover image.
- isImageSquare() : bool
- Tests if an image Asset is square.
- lqip() : string
- Returns a Low Quality Image Placeholder (LQIP) as data URL.
- maskable() : Asset
- Creates a maskable icon from an image asset.
- minify() : Asset
- Minifying an asset (CSS or JS).
- minifyCss() : string
- Minifying a CSS string.
- minifyJs() : string
- Minifying a JavaScript string.
- resize() : Asset
- Resizes an image Asset to the given width or/and height.
- scssToCss() : string
- Compiles a SCSS string.
- toCss() : Asset
- Compiles a SCSS asset.
- url() : string
- Creates an URL.
- varDump() : void
- Dump variable (or Twig context).
- webp() : Asset
- Converts an image Asset to WebP format.
- buildDarkSourceHtml() : string
- Builds HTML dark "source" elements for the dark color-scheme variant of an image Asset.
- convert() : Asset
- Converts an image Asset to the given format.
- htmlAttributes() : string
- Builds the HTML attributes string from an array.
Properties
$builder
protected
Builder
$builder
$config
protected
Config
$config
Methods
__construct()
public
__construct(Builder $builder) : mixed
Parameters
- $builder : Builder
asset()
Creates an Asset (CSS, JS, images, etc.) from a path or an array of paths.
public
asset(string|array<string|int, mixed> $path[, array<string|int, mixed>|null $options = null ]) : Asset
Parameters
- $path : string|array<string|int, mixed>
-
File path or array of files path (relative from
assets/orstatic/dir). - $options : array<string|int, mixed>|null = null
Return values
Assetavif()
Converts an image Asset to AVIF format.
public
avif(Asset $asset[, int|null $quality = null ]) : Asset
Parameters
- $asset : Asset
- $quality : int|null = null
Return values
AssetcacheKey()
Builds a cache key from a variable.
public
cacheKey(array<string|int, mixed> $context, string $name[, object|array<string|int, mixed>|string|null $value = null ]) : string
The cache key is built from the name of the variable, its hash, the site language and build.
Parameters
- $context : array<string|int, mixed>
-
Twig context, used to get the site language and build.
- $name : string
-
Name of the variable to build the cache key from.
- $value : object|array<string|int, mixed>|string|null = null
-
The variable to build the cache key from.
Return values
stringdataurl()
Returns the data URL of an image.
public
dataurl(string|Asset $asset) : string
Parameters
- $asset : string|Asset
Return values
stringdominantColor()
Returns the dominant hex color of an image asset.
public
dominantColor(string|Asset $asset) : string
Parameters
- $asset : string|Asset
Return values
stringfingerprint()
Fingerprinting an asset.
public
fingerprint(string|Asset $asset) : Asset
Parameters
- $asset : string|Asset
Return values
AssetgetEnv()
Gets the value of an environment variable.
public
getEnv(string|null $var) : string|null
Parameters
- $var : string|null
Return values
string|nullgetFilters()
public
getFilters() : array<string|int, mixed>
Return values
array<string|int, mixed>getFunctions()
public
getFunctions() : mixed
getTests()
public
getTests() : mixed
hash()
Hashing an object, an array or a string (with algo, xxh128 by default).
public
hash(object|array<string|int, mixed>|string $data[, mixed $algo = 'xxh128' ]) : string
Parameters
- $data : object|array<string|int, mixed>|string
- $algo : mixed = 'xxh128'
Return values
stringhtml()
Creates the HTML element of an asset.
public
html(array<string|int, mixed> $context, Asset|array<int, Asset, attributes: ?array} > $assets[, array<string|int, mixed> $attributes = [] ][, array<string|int, mixed> $options = [] ]) : string
Parameters
- $context : array<string|int, mixed>
-
Twig context
-
$assets
: Asset|array<int, Asset, attributes: ?array
} > -
Asset or array of assets + attributes
- $attributes : array<string|int, mixed> = []
-
HTML attributes to add to the element
- $options : array<string|int, mixed> = []
-
Options: [ 'preload' => false, 'responsive' => false, 'formats' => [], ];
Tags
Return values
string —HTML element
htmlAudio()
Builds the HTML audio element of an audio Asset.
public
htmlAudio(array<string|int, mixed> $context, Asset $asset[, array<string|int, mixed> $attributes = [] ][, array<string|int, mixed> $options = [] ]) : string
Parameters
- $context : array<string|int, mixed>
- $asset : Asset
- $attributes : array<string|int, mixed> = []
- $options : array<string|int, mixed> = []
Return values
stringhtmlCss()
Builds the HTML link element of a CSS Asset.
public
htmlCss(array<string|int, mixed> $context, Asset $asset[, array<string|int, mixed> $attributes = [] ][, array<string|int, mixed> $options = [] ]) : string
Parameters
- $context : array<string|int, mixed>
- $asset : Asset
- $attributes : array<string|int, mixed> = []
- $options : array<string|int, mixed> = []
Return values
stringhtmlImage()
Builds the HTML img element of an image Asset.
public
htmlImage(array<string|int, mixed> $context, Asset $asset[, array<string|int, mixed> $attributes = [] ][, array<string|int, mixed> $options = [] ]) : string
Parameters
- $context : array<string|int, mixed>
- $asset : Asset
- $attributes : array<string|int, mixed> = []
- $options : array<string|int, mixed> = []
Return values
stringhtmlImageFromWebsite()
Builds the HTML img element from a website URL by extracting the image from meta tags.
public
htmlImageFromWebsite(array<string|int, mixed> $context, string $url[, array<string|int, mixed> $attributes = [] ][, array<string|int, mixed> $options = [] ]) : string|null
Returns null if no image found.
Parameters
- $context : array<string|int, mixed>
- $url : string
- $attributes : array<string|int, mixed> = []
- $options : array<string|int, mixed> = []
Tags
Return values
string|nullhtmlJs()
Builds the HTML script element of a JS Asset.
public
htmlJs(array<string|int, mixed> $context, Asset $asset[, array<string|int, mixed> $attributes = [] ][, array<string|int, mixed> $options = [] ]) : string
Parameters
- $context : array<string|int, mixed>
- $asset : Asset
- $attributes : array<string|int, mixed> = []
- $options : array<string|int, mixed> = []
Return values
stringhtmlVideo()
Builds the HTML video element of a video Asset.
public
htmlVideo(array<string|int, mixed> $context, Asset $asset[, array<string|int, mixed> $attributes = [] ][, array<string|int, mixed> $options = [] ]) : string
Parameters
- $context : array<string|int, mixed>
- $asset : Asset
- $attributes : array<string|int, mixed> = []
- $options : array<string|int, mixed> = []
Return values
stringimageSizes()
Returns the HTML img `sizes` attribute based on a CSS class name.
public
imageSizes(string $class) : string
Parameters
- $class : string
Return values
stringimageSrcset()
Builds the HTML img `srcset` (responsive) attribute of an image Asset, based on configured widths.
public
imageSrcset(Asset $asset) : string
Parameters
- $asset : Asset
Tags
Return values
stringinline()
Returns the content of an asset.
public
inline(Asset $asset) : string
Parameters
- $asset : Asset
Return values
stringintegrity()
Hashing an asset with algo (sha384 by default).
public
integrity(string|Asset $asset[, string $algo = 'sha384' ]) : string
Parameters
- $asset : string|Asset
- $algo : string = 'sha384'
Return values
stringisAsset()
Tests if a variable is an Asset.
public
isAsset(mixed $variable) : bool
Parameters
- $variable : mixed
Return values
boolisImageLarge()
Tests if an image Asset is large enough to be used as a cover image.
public
isImageLarge(Asset $asset) : bool
A large image is defined as having a width >= 600px and height >= 315px.
Parameters
- $asset : Asset
Return values
boolisImageSquare()
Tests if an image Asset is square.
public
isImageSquare(Asset $asset) : bool
A square image is defined as having the same width and height.
Parameters
- $asset : Asset
Return values
boollqip()
Returns a Low Quality Image Placeholder (LQIP) as data URL.
public
lqip(string|Asset $asset) : string
Parameters
- $asset : string|Asset
Return values
stringmaskable()
Creates a maskable icon from an image asset.
public
maskable(string|Asset $asset[, int|null $padding = null ]) : Asset
The maskable icon is used for Progressive Web Apps (PWAs).
Parameters
- $asset : string|Asset
- $padding : int|null = null
Return values
Assetminify()
Minifying an asset (CSS or JS).
public
minify(string|Asset $asset) : Asset
Parameters
- $asset : string|Asset
Return values
AssetminifyCss()
Minifying a CSS string.
public
minifyCss(string|null $value) : string
Parameters
- $value : string|null
Return values
stringminifyJs()
Minifying a JavaScript string.
public
minifyJs(string|null $value) : string
Parameters
- $value : string|null
Return values
stringresize()
Resizes an image Asset to the given width or/and height.
public
resize(string|Asset $asset[, int|null $width = null ][, int|null $height = null ][, bool $remove_animation = false ]) : Asset
- If only the width is specified, the height is calculated to preserve the aspect ratio
- If only the height is specified, the width is calculated to preserve the aspect ratio
- If both width and height are specified, the image is resized to fit within the given dimensions, image is cropped and centered if necessary
- If remove_animation is true, any animation in the image (e.g., GIF) will be removed.
Parameters
- $asset : string|Asset
- $width : int|null = null
- $height : int|null = null
- $remove_animation : bool = false
Return values
AssetscssToCss()
Compiles a SCSS string.
public
scssToCss(string|null $value) : string
Parameters
- $value : string|null
Tags
Return values
stringtoCss()
Compiles a SCSS asset.
public
toCss(string|Asset $asset) : Asset
Parameters
- $asset : string|Asset
Return values
Asseturl()
Creates an URL.
public
url(array<string|int, mixed> $context[, Page|Asset|string|null $value = null ][, array<string|int, mixed>|null $options = null ]) : string
$options[ 'canonical' => false, 'format' => 'html', 'language' => null, ];
Parameters
- $context : array<string|int, mixed>
- $value : Page|Asset|string|null = null
- $options : array<string|int, mixed>|null = null
Return values
stringvarDump()
Dump variable (or Twig context).
public
varDump(Environment $env, array<string|int, mixed> $context[, mixed $var = null ][, array<string|int, mixed>|null $options = null ]) : void
Parameters
- $env : Environment
- $context : array<string|int, mixed>
- $var : mixed = null
- $options : array<string|int, mixed>|null = null
webp()
Converts an image Asset to WebP format.
public
webp(Asset $asset[, int|null $quality = null ]) : Asset
Parameters
- $asset : Asset
- $quality : int|null = null
Return values
AssetbuildDarkSourceHtml()
Builds HTML dark "source" elements for the dark color-scheme variant of an image Asset.
private
buildDarkSourceHtml(Asset $asset, array<string|int, mixed> $formats, mixed $responsive, array<string|int, mixed> $attributes) : string
Parameters
- $asset : Asset
- $formats : array<string|int, mixed>
-
Alternative formats (e.g. ['avif', 'webp'])
- $responsive : mixed
-
Responsive mode (true, 'width', 'density' or false)
- $attributes : array<string|int, mixed>
-
Image attributes
Return values
stringconvert()
Converts an image Asset to the given format.
private
convert(Asset $asset, string $format[, int|null $quality = null ]) : Asset
Parameters
- $asset : Asset
- $format : string
- $quality : int|null = null
Tags
Return values
AssethtmlAttributes()
Builds the HTML attributes string from an array.
private
static htmlAttributes(array<string|int, mixed> $attributes) : string
Parameters
- $attributes : array<string|int, mixed>