Last active
October 7, 2020 13:18
-
-
Save michsch/9ba3d1d3348294bf65e1d813876c8915 to your computer and use it in GitHub Desktop.
Nunjucks with custom filter for number_format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"paths": { | |
"nunjucks": "../Vendor/nunjucks/dist/js/nunjucks" | |
}, | |
"map": { | |
"*": { | |
"nunjucks": "Service/Factory/nunjucksFactory/nunjucksFactory" | |
}, | |
"Service/Factory/nunjucksFactory/nunjucksFactory" : { | |
"nunjucks" : "nunjucks" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define(['nunjucks', 'nunjucksEnv'], function (nunjucks, nunjucksEnv) { | |
'use strict'; | |
var numberFormat; | |
numberFormat = function (value, decimalNumbers, decimalSeparator, thousandSeparator) { | |
var newNumberAsString; | |
if (value == null || typeof value !== 'number' isNaN(value)) { | |
value = 0; | |
} | |
newNumberAsString = value.toFixed(decimalNumbers).toString(); | |
if (decimalSeparator != null) { | |
newNumberAsString = newNumberAsString.replace(/\./g, decimalSeparator); | |
} | |
if (thousandSeperator != null) { | |
newNumberAsString = newNumberAsString.replace(/\B(?=(\d{3})+(?!\d))/g, thousandSeparator); | |
} | |
return newNumberAsString; | |
}; | |
nunjucks.configure({ autoescape: true }); | |
nunjucksEnv.addFilter('number_format', numberFormat); | |
return nunjucks; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<span itemprop="price" content="{{ price | number_format(2, '.') }}"> | |
{{ price | number_format(2, ',', '.') }} | |
</span> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi. Variable name spelling mistake - https://gist.github.com/michsch/9ba3d1d3348294bf65e1d813876c8915#file-nunjucksfactory-js-L19