Reputation: 391
I know how to get the first letter of a string in TWIG
<p>The first letter is {{someString | first}}</p>
With an HTML string like
<p>This is a sting</p>
The above would return '<'
Adding 'Raw'/'escape' ends up to the same result.
I need to display that string as HTML ( like with Raw ) but get the first letter ( in the case above case 'T').
Am I using the filter in the incorrect order ?
Anyone knows ?
Many thanks ahead
@Matteo's answer is already close but not quite exactly what I wanted . I may have formulated my question wrongly. Sorry.
So If I had a string like this
<p>This is a <strong>string</strong></p>
Using raw would give
This is a string
Now what I really need is to get the first letter (T) to do something with it like adding tags around it
<span>T</span>his is a <strong>string</string>
while still keeping the rest of the HTML inside . Striptags remove all the tags in the string and return a plain string without the HTML part. I hope I formulated it right.
Upvotes: 4
Views: 1279
Reputation: 7764
How about this:
{% set someString = "This is a <strong>String<strong>"%}
<span>{{someString|striptags|first}}</span>{{someString|slice[4:]|raw}}
You can try in twigFiddle: http://twigfiddle.com/pk10ip
Upvotes: 0