15
15
class Twig_Extension_Core extends Twig_Extension
16
16
{
17
17
protected $ dateFormat = 'F j, Y H:i ' ;
18
+ protected $ numberFormat = array (0 , '. ' , ', ' );
18
19
19
20
/**
20
21
* Sets the default format to be used by the date filter.
@@ -36,6 +37,28 @@ public function getDateFormat()
36
37
return $ this ->dateFormat ;
37
38
}
38
39
40
+ /**
41
+ * Sets the default format to be used by the number_format filter.
42
+ *
43
+ * @param integer $decimal The number of decimal places to use.
44
+ * @param string $decimalPoint The character(s) to use for the decimal point.
45
+ * @param string $thousandSep The character(s) to use for the thousands separator.
46
+ */
47
+ public function setNumberFormat ($ decimal , $ decimalPoint , $ thousandSep )
48
+ {
49
+ $ this ->numberFormat = array ($ decimal , $ decimalPoint , $ thousandSep );
50
+ }
51
+
52
+ /**
53
+ * Get the default format used by the number_format filter.
54
+ *
55
+ * @return array The arguments for number_format()
56
+ */
57
+ public function getNumberFormat ()
58
+ {
59
+ return $ this ->numberFormat ;
60
+ }
61
+
39
62
/**
40
63
* Returns the token parser instance to add to the existing list.
41
64
*
@@ -73,6 +96,7 @@ public function getFilters()
73
96
'date ' => new Twig_Filter_Function ('twig_date_format_filter ' , array ('needs_environment ' => true )),
74
97
'format ' => new Twig_Filter_Function ('sprintf ' ),
75
98
'replace ' => new Twig_Filter_Function ('strtr ' ),
99
+ 'number_format ' => new Twig_Filter_Function ('twig_number_format_filter ' , array ('needs_environment ' => true )),
76
100
77
101
// encoding
78
102
'url_encode ' => new Twig_Filter_Function ('twig_urlencode_filter ' ),
@@ -310,6 +334,36 @@ function twig_date_format_filter(Twig_Environment $env, $date, $format = null, $
310
334
return $ date ->format ($ format );
311
335
}
312
336
337
+ /**
338
+ * Number format filter.
339
+ *
340
+ * All of the formatting options can be left null, in that case the defaults will
341
+ * be used. Supplying any of the parameters will override the defaults set in the
342
+ * environment object.
343
+ *
344
+ * @param Twig_Environment $env A Twig_Environment instance
345
+ * @param mixed $number A float/int/string of the number to format
346
+ * @param int $decimal The number of decimal points to display.
347
+ * @param string $decimalPoint The character(s) to use for the decimal point.
348
+ * @param string $thousandSep The character(s) to use for the thousands separator.
349
+ *
350
+ * @return string The formatted number
351
+ */
352
+ function twig_number_format_filter (Twig_Environment $ env , $ number , $ decimal = null , $ decimalPoint = null , $ thousandSep = null )
353
+ {
354
+ $ defaults = $ env ->getExtension ('core ' )->getNumberFormat ();
355
+ if ($ decimal === null ) {
356
+ $ decimal = $ defaults [0 ];
357
+ }
358
+ if ($ decimalPoint === null ) {
359
+ $ decimalPoint = $ defaults [1 ];
360
+ }
361
+ if ($ thousandSep === null ) {
362
+ $ thousandSep = $ defaults [2 ];
363
+ }
364
+ return number_format ((float ) $ number , $ decimal , $ decimalPoint , $ thousandSep );
365
+ }
366
+
313
367
/**
314
368
* URL encodes a string.
315
369
*
0 commit comments