Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use sprintf in Twig to format a number?

Tags:

twig

symfony

I need to format a number. In PHP, I would use sprintf("%05d",$lineNumber).

In a Symfony 2 Twig template, what code should I use?

like image 362
ChocoDeveloper Avatar asked Jan 17 '13 05:01

ChocoDeveloper


Video Answer


2 Answers

If i was 4 before, then after adding this to your Twig template:

"%02d"|format(i)

...it will become 04.

like image 166
crmpicco Avatar answered Sep 20 '22 15:09

crmpicco


Twig provides lots of filters, take a look at format and number_format.

like image 26
LET'S DISMANTLE NATO Avatar answered Sep 18 '22 15:09

LET'S DISMANTLE NATO