Twig template cannot include php template

后端 未结 3 1398
鱼传尺愫
鱼传尺愫 2021-01-13 10:00

I encountered a problem, which for me is quite not clear and hard to understand. I have tried to make calendar widget, which is supposed to be display on every page on my si

相关标签:
3条回答
  • 2021-01-13 10:02

    Have you considered rendering your calendar php template by using {% render 'ApplicationBundle:Controller:action' %} and in the action rendering the php template?

    You might also render your calendar php in the action that render calendar twig and pass the output of the php template as a simple twig variable.

    Note: to display such a var, don't forget to do {{ var|raw }} if there is any html tag inside.

    Note2: as of symfony2.2, the render parameter as changed to {% render url('route_name') %}

    0 讨论(0)
  • 2021-01-13 10:14

    NOTE: snippets below are totally non-tested.

    http://twig.sensiolabs.org/doc/functions/date.html

    The function date seems to create \DateTime object.

    {% set now = date() %}
    {% set offset = date(now.format('Y/m/01')).format(w) %} {# weekday of 1st day #}
    {% set number = now.format('t') %} {# days in month #}
    {% set koniec = 7 - ((offset + number) % 7) %}
    {% set aktualny = now.format('n') %} {# today #}
    

    However, if you wants to include original php file (say 'calendar.php') in twig, you have to write extension to get it work.

    class CalendarExtension extends \Twig_Extension
    {
        private $pathToPhp; //store that where the php file is
    
        public function setPhpFile($pathToPhp)
        {
            $this->pathToPhp = $pathToPhp;
        }
    
        public function getFunctions()
        {
            return array(
                new \Twig_SimpleFunction('calendar', array($this, 'showCalendar'))
            );
        }
    
        public function showCalendar([put arguments here if you need])
        {
            ob_start();
            include ($this->pathToPhp);
            return ob_get_clean();
        }
    }
    

    To make above work, you should create "tagged" service in container.

    in app/config/config.yml
    
    services:
        calendar_twig_extension:
            class: __Namespace__\CalendarExtension
            calls:
                - [setPhpFile, [__path to your calendar.php__]]
            tags:
                - [name: twig.extension]
    

    words that double-underscored should be replaced:

    • _ _ Namespace _ _ : Namespace of CalendarExtension
    • _ _ path to your calendar.php _ _ : full path to your calendar.php. You can use parameters like %kernel.root_dir% and so on to manage your path project-relative.

    With these, you finally can simply write

    {{ calendar([arguments for CalendarExtension::showCalendar]) }}
    
    0 讨论(0)
  • 2021-01-13 10:25

    You cannot mix-and-match twig and php in a single Response (to do so would be circumventing part of the point of twig, which is to prevent designers from creating too much logic in the view).

    I think the Symfony documentation should/could be clearer about this (at the moment it basically says "enable them both and do what you like").

    If you embed another controller then you should be able to serve up a different Response and that Response can be php based.

    0 讨论(0)
提交回复
热议问题