user2818060
user2818060

Reputation: 845

How to select First String in Twig

what i Need :

Upvotes: 0

Views: 710

Answers (2)

João Alves
João Alves

Reputation: 1941

I think you are looking for the truncate filter from the Text Extension. http://twig.sensiolabs.org/doc/extensions/text.html

You can configure it by adding a service definition like this:

services:
 twig.extension.text:
     class: Twig_Extensions_Extension_Text
     tags:
         - { name: twig.extension }

Then you can apply the filter:

 {{ item.metadata.name|truncate(10) }}

The filter have some arguments, and the one that matters for you is the separator value(by default its "..." which is the value you want if the string have more that 10 characters)

Upvotes: 0

np87
np87

Reputation: 543

You can use the Split filter of Twig to match the whitespace character. It'll return an array containing your splitted string.

New in version 1.10.3: The split filter was added in Twig 1.10.3.
The split filter splits a string by the given delimiter and returns a list of strings:

{% set foo = "one,two,three"|split(',') %}
{# foo contains ['one', 'two', 'three'] #}

In your case you'll want to replace the comma by a whitespace | split(' ')

More info in the Split page of Twig documentation.

Upvotes: 2

Related Questions