4
votes

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.

2
Hi there. If you think @Matteo 's answer answered your question (which I think it did), can you mark it as the correct answer. Thanks!Alvin Bunk
It's close but not quite what I'm looking forR.Damasinoro
Use CSS p::first-letter.malcolm
Are you able to click edit on your post and re-word your question correctly as per your comment to @Matteo 's post? If not, maybe post a new question. Reading your comment, I still don't understand what you need. Why does String have to be bolded - it's not clear.Alvin Bunk

2 Answers

5
votes

You could use the striptags filter:

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

Here a working solutions

Hope this help

0
votes

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