twig how to render a twig template as a variable

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



twig how to render a twig template as a variable



I'm trying to render twig template as variable, using symfony. I have a 'sendAction' Controller, which uses the mailgun API to send emails to one or more mailing lists. Here is my code for the Controller:


public function sendAction(Request $request, Newsletter $newsletter, MailgunManager $mailgunManager)
$form = $this->createForm(SendForm::class);
$form->handleRequest($request);

$formData = array();

if ($form->isSubmitted() && $form->isValid())

$formData = $form->getData();

$mailingLists = $formData['mailingLists'];

foreach ($mailingLists as $list)

$mailgunManager->sendMail($list->getAddress(), $newsletter->getSubject(), 'test', $newsletter->getHtmlContent());

return $this->render('webapp/newsletter/sent.html.twig');


return $this->render('webapp/newsletter/send.html.twig', array(
'newsletter' => $newsletter,
'form' => $form->createView()
));

}



And here's my sendMail (mailgun) function:


public function sendMail($mailingList, $subject, $textBody, $htmlBody)

$mgClient = new Mailgun($this::APIKEY);

# Make the call to the client.
$mgClient->sendMessage($this::DOMAIN, array(
'from' => $this::SENDER,
'to' => $mailingList,
'subject' => $subject,
'text' => $textBody,
'html' => $htmlBody
));



I want my ' $newsletter->getHtmlContent()' to render template called 'newsletter.twig.html'. can anyone help me or point me in the right direction as to what I can do or Where I can find Tutorials or notes on what I am trying to do. the symfony documentation is quite vague.





You want to render the template newsletter.twig.html inside the template webapp/newsletter/send.html.twig ?
– Cid
Aug 10 at 10:02


newsletter.twig.html


webapp/newsletter/send.html.twig





You should just do something like e.g. $mailgunManager->sendMail($list->getAddress(), $newsletter->getSubject(), 'test', $this->render($newsletter->getTemplate()));
– DarkBee
Aug 10 at 10:02


$mailgunManager->sendMail($list->getAddress(), $newsletter->getSubject(), 'test', $this->render($newsletter->getTemplate()));





Welcome to stack overflow, in case an answer helps you, you can mark it as correct by clicking the tickmark on the left of the answer.
– Sujit Agarwal
Aug 10 at 10:09





@DarkBee: Why do you keep removing the php and twig tags? Don’t you think that the question is related to PHP or Twig?
– lxg
Aug 10 at 14:18


php


twig





@lxg I don't think so no. There is not a letter twig in here and it aint a php problem on itself.
– DarkBee
Aug 10 at 14:22


twig


php




2 Answers
2



You can use getContent() chained to your render function.


getContent()


return $this->render('webapp/newsletter/send.html.twig', array(
'newsletter' => $newsletter,
'form' => $form->createView()
))->getContent();





So you’re creating a SymfonyComponentHttpFoundationResponse object, just to get the rendered content? Nice hack. ;)
– lxg
Aug 10 at 10:06


SymfonyComponentHttpFoundationResponse





I guess you commented on the wrong answer ;)
– Sujit Agarwal
Aug 10 at 10:08





No I didn’t. The getContent() method is on the Response object returned by $this->render(). You’re effectively returning the response body.
– lxg
Aug 10 at 10:10



getContent()


Response


$this->render()





Yeah, didn't notice that initially, my bad :)
– Sujit Agarwal
Aug 10 at 10:11



Simply inject an instance of SymfonyBundleFrameworkBundleTemplatingEngineInterface into your action, and you’ll be able to use Twig directly:


SymfonyBundleFrameworkBundleTemplatingEngineInterface


public function sendAction(Request $request, EngineInterface $tplEngine, Newsletter $newsletter, MailgunManager $mailgunManager)

// ... other code
$html = $tplEngine->render('webapp/newsletter/send.html.twig', [
'newsletter' => $newsletter,
'form' => $form->createView()
]);



Note that $this->render() (in the controller action) will return an instance of SymfonyComponentHttpFoundationResponse, while $tplEngine->render() returns a HTML string.


$this->render()


SymfonyComponentHttpFoundationResponse


$tplEngine->render()





The thing is also need the subject and the text body to be injected into the template, as I have placeholders for those variables in the template.
– TheKid
Aug 10 at 11:15





Basically, I don"t the subscriber to just receive the email as just the html content, but receive it as the a template with all the other variables injected into the template.
– TheKid
Aug 10 at 11:25





You can simply pass additional values into the array which is the second parameter to the render method. They will be available as Twig variables by the name of the array key.
– lxg
Aug 10 at 11:57


render






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS