ttskch/twigged-swiftmessage-service-provider

Twig templated Swift_Message builder service provider for the Silex microframework.

2.1.0 2016-09-23 09:21 UTC

This package is not auto-updated.

Last update: 2024-12-17 22:16:43 UTC


README

Build Status Latest Stable Version Total Downloads

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.