ariatemplates/hashspace — pull request #153 by b-laporte (2014-04-28 21:42:28+00:00)
Title: add support of pipe and new operators - implements #123

This PR implements the proposal discussed in issue #123.

I actually had to remove the JavaScript bitwise operator (i.e. `|`) from hashspace expressions to be able to use the pipe as 'modifier operator' - but as it is a standard statement in other template languages (e.g. smarty or angular), I preferred to keep the pipe for modifiers and discard the bitwise operator.

For the rest, I think all is in the sample:
```
var klass=require("hsp/klass");

function changeCase(s,arg) {
if (arg==="upper") {
return (""+s).toUpperCase();
} else if (arg==="lower") {
return (""+s).toLowerCase();
}
return s;
}

var Sorter=klass({
$constructor:function(property) {
this.ascending=true;
this.pp=property;
},
sort:function(array) {
// copy array
var arr=[], pp=this.pp, ascending=this.ascending;
for (var i=0,sz=array.length;sz>i;i++) {
arr[i]=array[i];
}
// sort
arr.sort(function (a,b) {
if (a[pp]>b[pp]) {
return ascending? 1 : -1;
} else if (a[pp]==b[pp]) {
return 0;
} else {
return ascending? -1 : 1;
}
});
return arr;
},
toggleOrder:function() {
this.ascending=!this.ascending;
}
})

# template sample(d)

Message in capital letters:
{d.msg|changeCase:"upper"}

Message in lower case:
{d.msg|changeCase:"lower"}

{let fnSorter=new Sorter("firstName")}
Sorted list:

{foreach p in d.persons|fnSorter.sort}

{p.firstName} {p.lastName}

{/foreach}

Toggle sort order (current: {fnSorter.ascending? "ascending" : "descending"})

# /template

var data={
msg:"Hello Simpsons!",
persons:[
{firstName:"Homer",lastName:"Simpsons"},
{firstName:"Marge",lastName:"Simpsons"},
{firstName:"Bart"},
{firstName:"Lisa"},
{firstName:"Maggy"}
]
};

sample(data).render("output");
```

PS: I forgot to mention that operators are applied in the same order as they are written - so that
```
{foo.bar|upper|spacify}
// is equivalent to
{spacify(upper(foo.bar))}
```