Cecil

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

$builder  : Builder
$config  : Config

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

Methods

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/ or static/ dir).

$options : array<string|int, mixed>|null = null
Return values
Asset

avif()

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
Asset

cacheKey()

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
string

dataurl()

Returns the data URL of an image.

public dataurl(string|Asset $asset) : string
Parameters
$asset : string|Asset
Return values
string

dominantColor()

Returns the dominant hex color of an image asset.

public dominantColor(string|Asset $asset) : string
Parameters
$asset : string|Asset
Return values
string

fingerprint()

Fingerprinting an asset.

public fingerprint(string|Asset $asset) : Asset
Parameters
$asset : string|Asset
Return values
Asset

getEnv()

Gets the value of an environment variable.

public getEnv(string|null $var) : string|null
Parameters
$var : string|null
Return values
string|null

getFilters()

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
string

html()

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
throws
RuntimeException
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
string

htmlCss()

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
string

htmlImage()

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
string

htmlImageFromWebsite()

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
throws
RuntimeException
Return values
string|null

htmlJs()

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
string

htmlVideo()

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
string

imageSizes()

Returns the HTML img `sizes` attribute based on a CSS class name.

public imageSizes(string $class) : string
Parameters
$class : string
Return values
string

imageSrcset()

Builds the HTML img `srcset` (responsive) attribute of an image Asset, based on configured widths.

public imageSrcset(Asset $asset) : string
Parameters
$asset : Asset
Tags
throws
RuntimeException
Return values
string

inline()

Returns the content of an asset.

public inline(Asset $asset) : string
Parameters
$asset : Asset
Return values
string

integrity()

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
string

isAsset()

Tests if a variable is an Asset.

public isAsset(mixed $variable) : bool
Parameters
$variable : mixed
Return values
bool

isImageLarge()

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
bool

isImageSquare()

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
bool

lqip()

Returns a Low Quality Image Placeholder (LQIP) as data URL.

public lqip(string|Asset $asset) : string
Parameters
$asset : string|Asset
Return values
string

maskable()

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
Asset

minify()

Minifying an asset (CSS or JS).

public minify(string|Asset $asset) : Asset
Parameters
$asset : string|Asset
Return values
Asset

minifyCss()

Minifying a CSS string.

public minifyCss(string|null $value) : string
Parameters
$value : string|null
Return values
string

minifyJs()

Minifying a JavaScript string.

public minifyJs(string|null $value) : string
Parameters
$value : string|null
Return values
string

resize()

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
Asset

scssToCss()

Compiles a SCSS string.

public scssToCss(string|null $value) : string
Parameters
$value : string|null
Tags
throws
RuntimeException
Return values
string

toCss()

Compiles a SCSS asset.

public toCss(string|Asset $asset) : Asset
Parameters
$asset : string|Asset
Return values
Asset

url()

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
string

varDump()

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
Asset

buildDarkSourceHtml()

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
string

convert()

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
throws
RuntimeException
Return values
Asset

htmlAttributes()

Builds the HTML attributes string from an array.

private static htmlAttributes(array<string|int, mixed> $attributes) : string
Parameters
$attributes : array<string|int, mixed>
Return values
string
On this page

Search results