Twig Extra is a set of Twig extensions that provides additional Twig helpers.
Installation
Install the package using Composer and Symfony Flex:
composerrequiresylius/twig-extra
Features
Sort by
This extension allows you to sort an array of objects by a specific property.
classBook {publicfunction__construct() {publicstring $name, }}$books = [newBook('The Shining'),newBook('The Lord Of The Rings'),newBook('Dune'),newBook('Wuthering Heights'),newBook('Fahrenheit 451'),];
<ul>{% for book in books|sort_by('name') %} <li>{{ book.name }}</li>{% endif %}</ul>
. Dune
. Fahrenheit 451
. The Lord Of The Rings
. The Shining
. Wuthering Heights
You can also sort nested arrays.
$books = [ ['name'=>'The Shining'], ['name'=>'The Lord Of The Rings'], ['name'=>'Dune'], ['name'=>'Wuthering Heights'], ['name'=>'Fahrenheit 451'],];
You just need to encapsulate the key with [].
<ul>{% for book in books|sort_by('[name]') %} <li>{{ book.name }}</li>{% endif %}</ul>
Test HTML attribute
This Twig extension lets you add data attributes in a test environment or when debug mode is enabled. This makes it easy to identify your data in E2E tests while minimizing dependency on HTML changes.
Like the sylius_test_html_attribute Twig extension, this one allows you to add some data attributes in your test environment or when debug mode is enabled. This function adds the data attribute via the attr Twig variable on a form theme block.
<!-- Actual html output below depends on your form theme --><labelfor="book_title">Title</label><inputtype="text"id="book_title"name="title"data-test-title <!-- Thisistheaddeddataattribute-->/>