Twig:渲染与包含 [英] Twig: render vs include

查看:26
本文介绍了Twig:渲染与包含的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个在线商店.如果我使用树枝函数render"而不是include",我会遇到性能问题.

I am creating an online store. I have a performance problem if I use the twig function "render" instead of "include".

这是显示产品目录的代码:

Here is the code that displays a product catalog:

目录控制器:

<?php
// src/Acme/StoreBundle/Controller/Product/Catalog.php

namespace Acme\StoreBundle\Controller\Product;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class CatalogController extends Controller
{
    /**
     * @Template()
     */
    public function productAction(\Acme\StoreBundle\Entity\Product\Category $category)
    {
        $qb = $this->getDoctrine()
            ->getRepository('StoreBundle:Product')
            ->createQueryBuilder('product')
            ->select('partial product.{id, token, name}')
            ->innerJoin('product.categoryRelation', 'categoryRelation')
            ->where('categoryRelation.category = :category_id');

        $qb->setParameters(array(
            'category_id'  => $category->getId(),
        ));

        $products = $qb->getQuery()
            ->getResult();

        return $this->render('StoreBundle:Product\Catalog:product.html.twig', array(
            'category' => $category,
            'products' => $products,
        ));
    }
}

... 目录控制器的模板:

... template for catalog controller:

{# src/Acme/StoreBundle/Resources/views/Product/Catalog/product.html.twig #}
{% extends 'AcmeDemoBundle::layout.html.twig' %}

{% block content %}
    <h1>{{ category.name }}</h1>

    <ul>
    {% for product in products %}
        <li>
            {#% render "StoreBundle:Product:show" with { product: product } %#}
            {% include "StoreBundle:Product:show.html.twig" with { product: product } %}
        </li>
    {% endfor %}
    </ul>

{% endblock %}

...产品控制器:

<?php
// src/Acme/StoreBundle/Controller/Product.php

namespace Acme\Enter\StoreBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

use Enter\StoreBundle\Entity\Product;

class ProductController extends Controller
{
    /**
     * @Template()
     */
    public function showAction(Product $product)
    {
        return array('product' => $product);
    }
}

... 用于产品控制器的简单(但将来会更复杂)模板:

... simple (but more complex in future) template for product controller:

{# src/Acme/StoreBundle/Resources/views/Product/show.html.twig #}
{{ product.name }}

所以如果我使用:

{% include "StoreBundle:Product:show.html.twig" with { product: product } %}

...一切正常:147ms 和 4608Kb 内存.

...all ok: 147ms and 4608Kb memory.

但是当我需要一个控制器来显示产品时:

But when I need a controller to display the product:

{% render "StoreBundle:Product:show" with { product: product } %#}

...我的脚本消耗了太多时间和内存:3639ms 和 17664Kb 内存!

...my script consumes too much time and memory: 3639ms and 17664Kb memory!

如何通过使用控制器来提高速度并减少内存消耗?

How to increase speed and reduce memory consumption by using the controller?

推荐答案

每个渲染调用都会产生一个新的请求,以及您所描述的性能下降问题.我认为除了使用 esi 缓存之外,您对此无能为力,因此可以缓存来自渲染调用的单个片段.否则,您可以尝试修改您的逻辑以减少渲染调用的使用.

Each render call spawns a new request, with the performance degradation issue that you are describing. I don't think there is much you can do about that but using esi caching, so that single fragments coming from render calls can be cached. Otherwise you could try to revise your logic to reduce the usage of render calls.

这篇关于Twig:渲染与包含的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆