Un mes de Symfony. Junio 2019

Lista de las principales novedades de Symfony durante junio de 2019

Image for post
Image for post
Un mes de Symfony. Junio 2019

Anuncio del Mailer Component

composer require symfony/amazon-mailer
AWS_ACCESS_KEY=...
AWS_SECRET_KEY=...
MAILER_DSN=smtp://$AWS_ACCESS_KEY:$AWS_SECRET_KEY@ses
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
class SomeService
{
private $mailer;
public function __construct(MailerInterface $mailer)
{
$this->mailer = $mailer;
}
public function sendNotification()
{
$email = (new Email())
->from('hello@example.com')
->to('you@example.com')
->subject('Time for Symfony Mailer!')
->text('Sending emails is fun again!')
->html('<p>See Twig integration for better HTML integration!</p>');
$this->mailer->send($email);
}
}

Twig añade los filtros filter, map y reduce

Filter

{% for product in products|filter(product => product.price > 10) %}
{# ... #}
{% endfor %}
{% for product in products|filter((key, product) => product.price > 10 and key % 2 == 0) %}
{# ... #}
{% endfor %}
{% for product in products|filter((k, p) => p.price > 10 and k % 2 == 0) %}
{# ... #}
{% endfor %}
{% for user in users if u.name is not empty %}
{% for user in users|filter(user => user.name is not empty) %}

Map

{% set people = [
{first: "Alice", last: "Dupond"},
{first: "Bob", last: "Smith"},
] %}

{{ people|map(p => p.first ~ ' ' ~ p.last)|join(', ') }}

Reduce

{% set values = [1, 2, 6] %}{% set sum = values|reduce((ac, currentElement) => ac + currentElement) %} Valor total {{ sum }} (9)

Macros en Twig

{% macro gray_box(content) %}
<div class="gray-box">
{{ content|raw }}
</div>
{% endmacro %}
{% macro gray_box(content) %}
<div class="gray-box">
{{ content|raw }}
</div>
{% endmacro %}
....{{ _self.gray_box('<p>Lorem ipsum</p>') }}

Lanzamiento de Twig 3

namespace App\Twig;


class AppExtension extends \Twig_Extension
{
public function getFilters()
{
return [
new \Twig_SimpleFilter('...', [$this, '...'])
];
}

// ...
}
namespace App\Twig;

use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

class AppExtension extends AbstractExtension
{
public function getFilters()
{
return [
new TwigFilter('...', [$this, '...']),
];
}

// ...
}

Mejoras en el control de espacios en blanco en Twig

Símbolo ~

<ul>
<li>
{% if some_expression %}
{{ some_variable }}
{% endif %}
</li>
</ul>
<ul>
<li>
Lorem Ipsum
</li>
</ul>
<ul>
<li>
{%- if some_expression %}
{{- some_variable -}}
{% endif -%}
</li>
</ul>
<ul>
<li>Lorem Ipsum</li>
</ul>
<ul>
<li>
{%~ if some_expression %}
{{ some_variable -}}
{% endif ~%}
</li>
</ul>
<ul>
<li>
Lorem Ipsum
</li>
</ul>

Filtro spaceless

{{ some_var|spaceless }}
{% spaceless %}
{# some HTML content here #}
{% endspaceless %}
{% apply spaceless %}
{# some HTML content here #}
{% endapply %}

Written by

Entre paseo y paseo con Simba desarrollo en Symfony y React

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store