Is there a way to use a Twig variable in a PHP filter in Twig Tweak?

I want to be able to process using PHP in my template, but I need to pull in a variable from the view.

For example {{ 'print title;' | php }} where title is equivalent to the Twig {{ title }} or as in {{ '$title = title; print title;' | php }}

Comments

mxwright created an issue. See original summary.

Chi’s picture

Category: Support request » Feature request

That is not supported yet. Though it's not a big deal to pass Twig context into PHP filter.

mxwright’s picture

Can you explain how to pass a Twig context into PHP? I don't necessarily need to use a Twig variable in the php code, but I need to be able to use a field (from Views) in it. I assume there's a way to do it, but I can't figure it out. Like {{ 'print $node->field_email->value;' | php }} or {{ 'print $twig->render(field_email);' | php }} - those are just guesses but I'm clearly missing something.

Chi’s picture

That requires changes in Twig Tweak PHP filter.
Also as you craft your code yourself you can do some crazy thins with PHP code string. Like follows.
{{ 'var_dump(' ~ node ~ ');' |php }}. Though I would not recommend it. It's much easier to achieve in preprocess hook.

  • Chi committed 47e2509 on 3.x
    Issue #3135932 by mxwright: Twig variable in PHP filter
    

  • Chi committed 21b6079 on 8.x-2.x
    Issue #3135932 by mxwright: Twig variable in PHP filter
    
Chi’s picture

Status: Active » Fixed

Fixed in Twig Tweak 2.6. Thanks for the report.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.