Twig: delimiter in the for tag

Is there any syntax to separate some elements in the for tag, please?

For example, I have a list of users and I want to show their username with a separator "-", so the expected result would be: Mickael - Dave - Chris - ...

I found this solution:

{% for user in entity.users %}
    {{ user.name }}{% if not loop.last %} - {% endif %}
{% endfor %}

But it is not very elegant. The join filter does not seem appropriate in a loop.

+4
source share
2 answers

AFAIK, , join . , getName . join Traversable glue.

, , .

__toString

User __toString, .
, toString,

namespace Acme\FooBundle\Entity;

class User
{
    public function __toString()
    {
        return $this->getName();
    }
}

join

{{ entity.users|join(' - ') }}

, , .
: , getUsers PersistentCollection, , array_map

// Acme/FooBundle/Controller/MyController#fooAction

$users = $entity->getUsers();
$usernames = $users->map(function(User $user) {
    return $user->getName();
});

return $this->render('AcmeFooBundle:My:foo.html.twig', array(
    'entity' => $entity,
    'usernames' => $usernames
);

{{ usernames|join(' - ') }}

TWIG

Twig Extension. joinBy, join, .

, , ,

services.yml

acme_foo.tools_extension:
    class: Acme\FooBundle\Twig\ToolsExtension
    tags:
        - { name: twig.extension }

@Acme\FooBundle\Twig\ToolsExtension

namespace Acme\FooBundle\Twig;

use Twig_Extension, Twig_Filter_Method;
use InvalidArgumentException, UnexpectedValueException;
use Traversable;

/**
 * Tools extension provides commons function
 */
class ToolsExtension extends Twig_Extension
{
    /**
     * {@inheritDoc}
     */
    public function getName()
    {
        return 'acme_foo_tools_extension';
    }

    /**
     * {@inheritDoc}
     */
    public function getFilters()
    {
        return array(
            'joinBy' => new Twig_Filter_Method($this, 'joinBy')
        );
    }

    /**
     * Implode-like by specifying a value of a traversable object
     *
     * @param mixed  $data  Traversable data
     * @param string $value Value or method to call
     * @param string $join  Join string
     *
     * @return string Joined data
     */
    public function joinBy($data, $value, $join = null)
    {
        if (!is_array($data) && !($data instanceof Traversable)) {
            throw new InvalidArgumentException(sprintf(
                "Expected array or instance of Traversable for ToolsExtension::joinBy, got %s",
                gettype($data)
            ));
        }

        $formatted = array();

        foreach ($data as $row) {
            $formatted[] = $this->getInput($row, $value);
        }

        return implode($formatted, $join);
    }

    /**
     * Fetches the input of a given property
     *
     * @param  mixed  $row  An array or an object
     * @param  string $find Property to find
     * @return mixed  Property value
     *
     * @throws UnexpectedValueException When no matching with $find where found
     */
    protected function getInput($row, $find)
    {
        if (is_array($row) && array_key_exists($find, $row)) {
            return $row[$find];
        }

        if (is_object($row)) {
            if (isset($row->$find)) {
                return $row->$find;
            }

            if (method_exists($row, $find)) {
                return $row->$find();
            }

            foreach (array('get%s', 'is%s') as $indic) {
                $method = sprintf($indic, $find);

                if (method_exists($row, $method)) {
                    return $row->$method();
                }
            }

            if (method_exists($row, $method)) {
                return $row->$method();
            }
        }

        throw new UnexpectedValueException(sprintf(
            "Could not find any method to resolve \"%s\" for %s",
            $find,
            is_array($row)
                ? 'Array'
                : sprintf('Object(%s)', get_class($row))
        ));
    }
}

,

{{ entity.users|joinBy('name', ' - ') }}
# or
{{ entity.users|joinBy('getName', ' - ') }}
+8

__toString() Entity, .

Twig join, {{ entity.users|join(' - ') }}.

+1

All Articles