Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TWIG how to get the first word of a string?

Tags:

twig

symfony

I've got a string and I want to

  • isolate the first word for styling with style1 and
  • display the rest of the string with style2 without the first word.

Something like that :

<span class="style1">{{ string|firstword() }}</span>
<span class="style2">{{ string|restofstring() }}</span>

Is it possible ? Thank you in advance.

like image 263
Antoine Subit Avatar asked Jun 18 '14 09:06

Antoine Subit


Video Answer


1 Answers

I found it ! With split() and attribute() TWIG functions.

{% set array = article.titre|split(' ', 2) %}
<span class="style1">{{ attribute(array, 0) }}</span><!-- First word -->
<span class="style2">{{ attribute(array, 1) }}</span><!-- Rest of string -->

Thanks to Anjana Silva who give me the begginning of the idea.

like image 99
Antoine Subit Avatar answered Oct 02 '22 03:10

Antoine Subit