Mattias Sundberg

Mattias Sundberg 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 här.

Senaste inläggen

Nyligen kommenterat

Andra sidor

Visa min profil på LinkedIn.com
Bloggportalen.se
Mashit.se

Visar inlägg taggade med trimpath

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.
"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); }
}

AJAJ

So all the hype is about AJAX, an acrynom every programmer is familiar with by now. However, I use AJAJ and I think most sites using AJAX really is using AJAJ.

So what is AJAJ then? Well, basically the same as AJAX but the last J stands for JSON. JSON has proven to be most users choice since it provides less overhead than XML. Personally I send raw JSON-encoded database resultsets with AJAX(J) and use TrimPath to get rid of manual DOM-tree manipulation. For those familiar with the Smarty template system, TrimPath is very easy to get started with.

Leaner and lighter

Stripped away about 200 lines of Javascript code today. Mostly obsolete stuff that wasn't in use but also some effects like the sortable "modules" in the right column. No need for it really.

The size of the file shrunk to 12Kb. But there's still about 96Kb of Javascript being loaded, Trimpath and Prototype being the largest. I can probably shrink that a lot by using the light version of Prototype but that mean I have to change a lot of the Ajax stuff so we'll se about that one...

New module

Wrote a new module last night.
The module is called mod_xmms and displays the current tracks being played in Xmms. However, it´s a quite custom module since it will not work unless Xmms is run from the same machine as the blog engine. It also shows the cover image from my Mp3 Archive which is my other ongoing project.

I also now load the jstemplates with Ajax and I am also able to reload the templates with Ajax after editing them. Quite handy if you ask me =)