如何在Twig模板中插入值并将纯文本用作字符串


How to Insert values in a Twig template and use the plain text as a string?

我希望能够发送html电子邮件。我用trick创建了模板,其中有一些变量{{ name }} {{ date }}等等。我现在想做的是将php脚本中的变量传递到模板中,并使用带有注入值的新html文件作为字符串,我将其传递到mail()。我怎样才能做到这一点?我试过

  $emailText = $this->container['view']->render('user/mailTemplate.html', array('name' => $username, 'date'=>$currentdate));

但我得到了"消息:参数1传递到Slim''Views''Twig::render(("必须实现接口Psr''Http''Message''ResponseInterface的

使用Slim Framework Twig View组件可以获得fetchFromString()方法:

$rendered = $this->view->fetchFromString(
    $template_string, 
    array('fname' => 'John',
        'lname' => 'Doe')
);

或者使用->fetch()从文件中加载模板(如果它在模板目录中(。