Monday, November 24, 2025
HomePHPNew in Twig 3.15 (half 2) (Symfony Weblog)

New in Twig 3.15 (half 2) (Symfony Weblog)


Within the first a part of this weblog put up we launched thrilling new Twig options
like inline feedback, PHP enums help, improved operator priority, the guard
tag, and enhanced deprecation dealing with. This second half highlights different good
current additions to Twig.

The attribute() perform permits accessing attributes or properties of variables
or objects the place the title of the attribute/property is dynamic:

In Twig 3.15, the attribute() perform is deprecated, and the dot operator
has been prolonged to deal with dynamic properties:

Though the attribute() is deprecated, it’s going to stay accessible in Twig 4.0
to make sure a smoother improve path.


Nicolas Grekas
Contributed by
Nicolas Grekas
in
#4353

One other enhancement to the dot operator is the flexibility to entry class constants
instantly from an object:

Twig macros are much like capabilities in programming languages and they’re
helpful to reuse template fragments. Ranging from Twig 3.15, you should utilize
named arguments when calling a macro, with the syntax argument: worth,
similar to in PHP:

Named arguments are additionally supported when passing arguments with the dot operator:

With these adjustments, named arguments at the moment are supported in all places: capabilities,
filters, exams, macros, and dot operator arguments.

Beforehand, Twig required snake_case argument names when calling PHP callables,
no matter their authentic signature. In Twig 3.15, you should utilize both snake_case
or camelCase, regardless of which format is utilized by the PHP signature:

The ... unfold operator can now broaden arguments when calling a perform:

Arrow capabilities, a favourite of many builders, at the moment are supported in all places
in Twig. Beforehand supported in filters and capabilities, they’ll now even be used
in exams, macro arguments, and technique name arguments:

Arrow capabilities can now even be saved in Twig variables:




RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments