Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twig equivalent of lcfirst

Tags:

twig

I'm not able to find a equivalent of lcfirst php function in Twig, my need is to lower only the first letter of a word ? If such function doesn't exist, what is the best way to do it ?

like image 454
TikTaZ Avatar asked Oct 28 '25 03:10

TikTaZ


3 Answers

As discussed in this issue on Github, you could use:

{{ foo[:1]|lower ~ foo[1:] }}

See this working example.

like image 52
Matteo Avatar answered Oct 30 '25 09:10

Matteo


Just add the function into twig by chaining it with a filter, e.g.

$twig->addFilter(new \Twig\TwigFilter('lcfirst', 'lcfirst'));

Then use it inside any twig template like

{{ string | lcfirst }}
like image 24
DarkBee Avatar answered Oct 30 '25 11:10

DarkBee


As discussed in this issue on Github, you could use:

{{ foo[:1]|lower ~ foo[1:] }}

See this working example.

like image 43
Matteo Avatar answered Oct 30 '25 11:10

Matteo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!