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

时间:2016-08-08 09:37:45

标签: php twig slim

我希望能够发送HTML电子邮件。我用twig创建了模板,我在其中有一些变量{{ 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'

1 个答案:

答案 0 :(得分:0)

使用 Slim Framework Twig View 组件为您提供 fetchFromString() 方法:

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

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