Note: Dust documentation makes the following assertion when you use filters: "all output values are escaped to avoid Cross Site Scripting (XSS) unless you use filters.", but only applies to the default filters that brings Dust. If you create your own filters, they are still protected against XSS unless used in conjunction with the default Dust filters.
First, If you look into Dust source code you will find that the next filters are already defined:
In order to extend Dust filters without touching the source code I will use jQuery extend, you can use others frameworks like Underscore.js ( _.extend function ) too, but I feel very comfortable with jQuery so, here we go.
jQuery extend description: Merge the contents of two or more objects together into the first object. When two or more object arguments are supplied to
jQuery.extend(), properties from all of the objects are added to the target object. Arguments that are
undefined are ignored.
That means that is possible to read dust.filters object with jQuery.extend and merge it with a new object (my new filter):
If you are going to add more than one filter, you can created a separate object as the container of your new filters and pass it to jQuery.extend instead of create them inside jQuery extend.
Don't forget to call the Dust filters extension after Dust is already called.
Now you can use you new filter in the same way as the original Dust filters:
As I mentioned before, If you create your own filters they are still protected against XSS, but be careful, Dust filters can be chained, that means that you can use more of one filter at the same time, as the next example:
In this particular case title is NOT LONGER protected again XSS since the filter --> " |s " suppresses auto-escaping, so, be careful in the way you chain filters, specially with the default filters.
Also, is possible to replicate the same functionality as a helper:
Your template will looks like:
Hope you find this information useful!