How to create an extension twig in symfony that returns the day of the week?

0

How you can create an extension in twig, that does the same as this PHP code:

public function nombredia($nombredia)
{

    $dias = array('','Lunes','Martes','Miercoles','Jueves','Viernes','Sabado','Domingo');

    $fecha = $dias[date('N', strtotime($nombredia))];

    return $fecha;

}
    
asked by juanitourquiza 26.05.2017 в 00:32
source

1 answer

0

To perform this action you must create an extension that must be located in AppBundle / Twig / NombrediaExtension.php with the following code:

<?php

namespace AppBundle\Twig;


//Extension que saca el nombre del dia
class NombreDiaExtension extends \Twig_Extension
{

public function getFunctions()
{

    return array(
    new \Twig_SimpleFunction('nombre_dia', array($this, 'nombre_dia')),
    );

}


public function getName()
{

    return 'nombre_dia_extension';

}


public function nombre_dia($nombredia)
{

    $dias = array('','Lunes','Martes','Miercoles','Jueves','Viernes','Sabado','Domingo');

    $fecha = $dias[date('N', strtotime($nombredia))];

    return $fecha;

 }
}

After this we must place the following code inside: app / config / services.yml

services:
#service_name:
#    class: AppBundle\Directory\ClassName
#    arguments: ['@another_service_name', 'plain_value', '%parameter_name%']

app.twig_extension.nombre_dia_extension:
    class: AppBundle\Twig\NombreDiaExtension
    tags:
        - { name: twig.extension }

Finally in the twig file that we are using we make one of this function:

{% set diasemana = nombre_dia(datos.DueDate|date("Y-m-d")) %}
{{diasemana }}

Greetings

    
answered by 26.05.2017 / 00:32
source