N
N
Nikolai Egorov2018-11-26 13:49:33
symfony
Nikolai Egorov, 2018-11-26 13:49:33

How to get the template code in twig without parsing it by twig itself?

I have ajax loaded blocks on my site. There are several. Some of them require substitution of some data or generation, some do not - static html code. When the page is opened, one ajax request is sent to receive data for all these blocks at once (then in a loop I insert data for the corresponding blocks in the page).
To save resources, those blocks that do not require processing / generation by twig, I get this way: `'why-we' => $twig->load('public/partials/why-we.html.twig') ->getSourceContext()->getCode()`. At the same time, as I understand it, twig does not do semantic parsing of the template, at least the entire twig code remains unchanged. This works in the `dev` environment, but somehow doesn't work in 'prod' - which returns an empty string instead of the template code.
I don't understand why it doesn't work, please help! Maybe it's not worth it at all, and just get the html code in the same way as the one generated by twig, i.e. method `$this->render('public/partials/xxx.html.twig')->getContent()`
Controller code

public function loadData(Request $request, \Twig_Environment $twig)
    {
        // проверяем на попытку добавить в URI посторонние параметры
        if ($request->getRequestUri() != $this->generateUrl($request->get('_route'))) {
            return $this->redirectToRoute('homepage', [], 301);
        }

        $results = [
            // just load HTML content
            'why-we' => $twig->load('public/partials/why-we.html.twig')->getSourceContext()->getCode(),
            'note-consult' => $twig->load('public/partials/note-consultation.html.twig')->getSourceContext()->getCode(),
            'advantages' => $twig->load('public/partials/advantages.html.twig')->getSourceContext()->getCode(),
            'faq-note' => $twig->load('public/partials/note-faq.html.twig')->getSourceContext()->getCode(),
            'faq-snippets' => $twig->load('public/partials/faq-snippets.html.twig')->getSourceContext()->getCode(),

            // render template
            'block-phone' => $this->render('public/partials/phone-under-content.html.twig')->getContent(),
        ];

        return $this->json($results);
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question