Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to embed php in twig

Tags:

php

twig

symfony

I have a do_shortcut and I need to embed it in a twig template. I tried by coping the code in a php file my-code.php:

<?php do_shortcut('[my-code]'); ?>

Next, in the twig page over.twig:

   {{ include ('options/my-code.php') }}

/* I also tried */

   {% php %}
            <?php do_shortcut('[my-code]'); ?>
   {% endphp %}

But doesn't work. Any suggestion? Thanks.

like image 224
user3051319 Avatar asked Nov 30 '13 05:11

user3051319


People also ask

Can you put PHP in a Twig file?

Symfony defaults to Twig for its template engine, but you can still use plain PHP code if you want.

Is Twig a Symfony?

Twig is a PHP template engine. It was created by Symfony developers.

How do I get a Twig URL?

You can get the current URL in Twig/Silex 2 like this: global. request. attributes. get('_route') .

Is Twig a programming language?

Twig is a template engine for the PHP programming language. Its syntax originates from Jinja and Django templates. It's an open source product licensed under a BSD License and maintained by Fabien Potencier. The initial version was created by Armin Ronacher.


1 Answers

You can't do that, you should create a twig extension and transform the php function into a twig function: http://symfony.com/doc/current/cookbook/templating/twig_extension.html

like image 110
Wouter J Avatar answered Sep 19 '22 13:09

Wouter J