首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Symfony & twig使用nochso/ html -compress-twig缩小html

Symfony & twig使用nochso/ html -compress-twig缩小html
EN

Stack Overflow用户
提问于 2017-11-23 22:43:38
回答 2查看 1.8K关注 0票数 0

正如我在How can I minify HTML with Twig?上看到的

建议使用https://github.com/nochso/html-compress-twig缩小从Twig模板生成的html。

但在文档中,它没有显示使用Symfony加载的方法。你们知道如何在Symfony中使用它吗?

据它所说:

代码语言:javascript
复制
$twig = new Twig_Environment($loader);
$twig->addExtension(new \nochso\HtmlCompressTwig\Extension());

但是在Symfony上,我如何获得现有的Twig_Environment,以及将扩展初始化放在哪里?

EN

回答 2

Stack Overflow用户

发布于 2018-01-16 18:45:37

安装后,尝试将其注册为服务:

代码语言:javascript
复制
services: 
    nochso\HtmlCompressTwig\Extension
        tags:
            - { name: twig.extension }
票数 1
EN

Stack Overflow用户

发布于 2018-04-19 18:54:04

例如,您的src/Controller目录中有BaseController。

控制器应该从控制器类的Controller

  • Override

  • 方法创建

  • ,并在每个控制器

中使用此方法

代码语言:javascript
复制
class BaseController extends Controller {
protected function render($view, array $parameters = array(), Response $response = null)
    {
        if ($this->container->has('templating')) {
            $content = $this->container->get('templating')->render($view, $parameters);
        } elseif ($this->container->has('twig')) {
            $content = $this->container->get('twig')->render($view, $parameters);
        } else {
            throw new \LogicException('You can not use the "render" method if the Templating Component or the Twig Bundle are not available. Try running "composer require symfony/twig-bundle".');
        }

        if (null === $response) {
            $response = new Response();
        }
        $content = preg_replace(array('/<!--(.*)-->/Uis',"/[[:blank:]]+/"),array('',' '),str_replace(array("\n","\r","\t"),'',$content));
        $response->setContent($content);

        return $response;
    }
}

您还可以在其他控制器中扩展BaseController。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47458058

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档