Skip to main content

Add custom functions and variables to Twig

To add a custom function use following filter:

add_filter( 'integration-cds/twig/functions', function( $functions ) {
$functions['translate'] = new \Twig\TwigFunction(
'translate',
function ( $text ) {
return __( $text );
}
);

return $functions;
}, 10, 1 );

Usage:

{{ translate('Hello') }}

To add a custom variable use following action:

add_action( 'integration-cds/twig/after-globals', function ( $twigEnvironment ) {
$twigEnvironment->addGlobal( 'today', date( 'm/d/Y' ) );
} );

Usage:

{{ today }}