R.Damasinoro
R.Damasinoro

Reputation: 391

TWIG Get first letter of HTML after Raw filter

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

Answers (2)

Alvin Bunk
Alvin Bunk

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

Matteo
Matteo

Reputation: 39380

You could use the striptags filter:

<p>The first letter is {{someString|striptags | first}}</p>

Here a working solutions

Hope this help

Upvotes: 5

Related Questions