简体   繁体   English

Silex,在服务中使用Twig

[英]Silex, using Twig in service

I'm creating an application in Silex. 我正在Silex中创建一个应用程序。 I want to use Twig to render templates in my service which is loading by Pimple. 我想使用Twig在服务中渲染模板,该服务由Pimple加载。 I need this for mailer class. 我在邮递员班上需要这个。

I started writing application based on silex-skeleton, so I have an twig environment in $app['twig']. 我开始基于silex-skeleton编写应用程序,因此我在$ app ['twig']中有一个树枝环境。 The problem is, when I want to pass it into a service: 问题是,当我想将其传递到服务中时:

//app.php
$app['someModel'] = function ($app) {
    return new someModel($app['twig']);
};

When I'm writing something like this, twig stops working. 当我写这样的东西时,树枝停止工作。 All my subpages show: 我所有的子页面都显示:

Twig_Error_Loader: Template "(path).html.twig" is not defined (). Twig_Error_Loader:模板“(path).html.twig”未定义()。

I've tried another trick: 我尝试了另一个技巧:

//app.php
$app['someModel'] = function ($app) {
    return new someModel($app);
};

//someModel.php
class SomeModel
{
    private $twig;

    public function __construct($app)
    {
        $this->twig = $app['twig'];
    }
}

but that also breaks the twig. 但这也折断了树枝。 I've tried to assign and clone an $app['twig'] to another variable, then it also happens. 我尝试将$ app ['twig']分配并克隆到另一个变量,然后它也会发生。

//app.php
$variable = $app['twig']

//app.php
$variable = clone $app['twig']

How can I use Twig in service in Silex? 如何在Silex的服务中使用Twig?

$app->register(new Silex\Provider\TwigServiceProvider(), 
  array('twig.path' => __DIR__.'/views',
));

To get more information have a look into the Silex Documentation . 要获取更多信息,请查看Silex文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM