TrimPath modifiers
Thought I'd share two TrimPath template modifiers with you. Two very simple modifiers but still quite handy.
First is a newline to <br/> modifier which is the one I use the most.
The second one is a trim-function that strips whitespaces in the beginning and the end of a string. This one requires a external function called trimAll().
And the code for trimAll().
To add the modifiers just add the lines of code in the TrimPath.parseTemplate_etc.modifierDef-section of the TrimPath-library. It should look something like this when you're done.
First is a newline to <br/> modifier which is the one I use the most.
"nl2br" : function(s) { return String(s).replace(/\n/g,"<br/>");}The second one is a trim-function that strips whitespaces in the beginning and the end of a string. This one requires a external function called trimAll().
"trim" : function(s) { return trimAll(s);}And the code for trimAll().
function trimAll(sString) {
while (sString.substring(0,1) == ' ') {
sString = sString.substring(1, sString.length);
}
while (sString.substring(sString.length-1, sString.length) == ' ') {
sString = sString.substring(0,sString.length-1);
}
return sString;
}To add the modifiers just add the lines of code in the TrimPath.parseTemplate_etc.modifierDef-section of the TrimPath-library. It should look something like this when you're done.
TrimPath.parseTemplate_etc.modifierDef = {
"eat":function(v) { return ""; },
"capitalize":function(s){ return String(s).toUpperCase(); },
"default":function(s, d) { return s != null ? s : d; },
"nl2br":function(s) { return String(s).replace(/\n/g,"<br/>");},
"trim":function(s) { return trimAll(s); }
}

Detta är min alldeles högst personliga blogg där jag skriver om än det ena än det andra.
Ibland skriver jag ofta(re) ibland väldigt sällan. Jag skriver delvis säsongsbetonat då jag är ett stort Formel 1-fan och delvis om mitt jobb/hobby som programmerare. Mer om mig hittar du 

Lämna en kommentar
Din e-postadress kommer inte att publiceras.