This is a service provider of TwiggedSwiftMessageBuilder for Silex.
- PHP 5.3+
First add this dependency into your composer.json
:
{
"require": {
"ttskch/twigged-swiftmessage-service-provider": "~2.0"
}
}
And enable this service provider in your application.
Please notice that you must register both TwigServiceProvider
and SwiftmailerServiceProvider
on ahead.
$app->register(new TwigServiceProvider());
$app->register(new SwiftmailerServiceProvider());
$app->register(new \Ttskch\Silex\Provider\TwiggedSwiftMessageServiceProvider());
Then you can build Swift_Message
object via twig template.
$message = $app['twigged_message']->buildMessage('email.txt.twig');
$message->setTo('[email protected]');
$app['mailer']->send($message);
See more detailed documentation here.
This service provider provides one additional feature to use submitted form data in Twig template easily.
$app['twigged_swiftmessage.form_handler']
allows you to extract data array from Form
object.
Labels and values of each fields can be used as below:
{{ form.[field-name].label }}
{{ form.[field-name].value }}
For example:
{# email.txt.twig #}
{% block from %}{{ form.email.value }}{% endblock %}
{% block from_name %}{{ form.name.value }}{% endblock %}
{% block to %}[email protected]{% endblock %}
{% block subject %}[Contact] {{ form.summary.value }}{% endblock %}
{% block body %}
{% for item in form %}
----------------------------------------------------------------------
{{ item.label }}: {{ item.value }}
{% endfor %}
----------------------------------------------------------------------
{% endblock %}
// in your controller.
$form->handleRequest($request);
if ($form->isValid()) {
$array = $app['twigged_message.form_handler']->getDataArray($form);
$message = $app['twigged_message']->buildMessage('email.txt.twig', array('form' => $array));
$app['mailer']->send($message);
}
Offcourse you also can access to your custom field type as below:
{{ form.[parent-field].[child-field].label }}
{{ form.[parent-field].[child-field].value }}
See more detailed documentation here.