How to embed php in twig

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.


你不能这样做,你应该创建一个树枝扩展,并将php函数转换为一个树枝函数:http://symfony.com/doc/current/cookbook/templating/twig_extension.html


About the include part, create a my_code.html.twig file at app/Resources/views/my_code.html.twig and copy-paste your code from my-code.php

Then you can include that code anywhere like :

{% include 'my_code.html.twig' %}

EDIT: tested and working in symfony3


试试这个代码:

{{ wp.do_shortcode('[shortcode]')|raw }} 
链接地址: http://www.djcxy.com/p/17540.html

上一篇: 如何捕捉S4方法选择期间发送的警告

下一篇: 如何嵌入在树枝中的PHP