M
M
magary42017-02-15 19:03:59
JavaScript
magary4, 2017-02-15 19:03:59

How to optimize iterative template rendering?

The controller that prepares the response for typeahead.js

foreach($items as &$item) {
     $item->html = $this->container->get("twig")->render(“line.html.twig”,[“item”=>$item]));
}

return new JsonResponse( $items );

the idea is that there are 15 items and you need to iterate and add the html representation to each element
but the code above runs 200ms longer than if $item->html = “”.$item->name.””
how to avoid a delay of 200ms ?
was done via >render("line.html.twig",["item"=>$item]));
because it's important to be able to override this line.html.twig in inherited bundles,
rendering on the client side is not suitable,
you need to either optimize it or tell me how to override it but without >render("
Thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis, 2017-02-15
@prototype_denis

$item->html = $this->container->get("twig")->render(“lines.html.twig”,[“items”=>$items]));

Draw the collection right away (Here, the twig cache will give performance)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question