Reputation: 845
what i Need :
i Need only First Name from the String.
Like String : Bodo Jorg Udo .. ( i have .. notation if string of length is more then 20).
here is source code
{%if item.metadata.name |length < 20 %}
{{item.metadata.name}}
{%else%}{{WordLimit(item.metadata.name,10,10)}} ..
{%endif%}
output should be like
Bodo
Upvotes: 0
Views: 710
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
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