Reputation: 2520
I am in the process of converting several websites from a raw PHP templating system over to Twig. One problem I have hit is how to convert templates where the PHP extract function is used. Any thoughts on how to handle this?
Upvotes: 4
Views: 1314
Reputation: 990
EDIT: not actually the correct solution to OP's problem, but this came up when I searched, before I remembered the solution, so posting it in case it helps anyone.
The with
tag is functionally equivalent to a scoped extract()
which can be used within a template.
https://twig.symfony.com/doc/3.x/tags/with.html
{% set example_array = {foo: 'bar'} %}
{% with example_array %}
{{ foo }}
{% endwith %}
Upvotes: -1
Reputation: 34105
Pass the array that you would otherwise call extract()
on to twig's render function, in the end it will behave exactly like you need - the array's keys as the variable names.
Upvotes: 0