qckanemoto / twigged-swiftmessage-service-provider
Twig templated Swift_Message builder service provider for the Silex microframework.
Requires
- php: >=5.3.3
- silex/silex: ~1.0
- symfony/form: ~2.3
- symfony/security-csrf: ~2.4
- ttskch/twigged-swiftmessage-builder: ~2.0
Requires (Dev)
- phake/phake: ~2.0@dev
- phpunit/phpunit: ~4.0
This package is not auto-updated.
Last update: 2022-02-01 12:41:33 UTC
README
This is a service provider of TwiggedSwiftMessageBuilder for Silex.
Requirements
- PHP 5.3+
Getting started
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('hoge@example.com'); $app['mailer']->send($message);
See more detailed documentation here.
Use submitted form data in Twig template
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 %}contact@example.com{% 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 }}
Enjoy!
See more detailed documentation here.