This PAX library contains a set of methods to be used in a PAX template to format template data, so that it appears uniform, eg: capitalise names.
Mikkel Bergmann, http://www.pointful.com
You should note that each method from this library is imported “locally” into each template, so that they can be referenced by their short name, for example “pax.template.modifier._def” can be referenced by just “_def”. All PAX examples utalise this short name facility. Due to this facility, you must be careful when implementing your own functions, so the names don’t clash. To make it easier, the template system will allow overriding of the methods, so that they have your expected functionality. This of course explains why each method starts with an underscore.
| pax. | This PAX library contains a set of methods to be used in a PAX template to format template data, so that it appears uniform, eg: capitalise names. |
| Functions | |
| _def | Returns defaultValue, if value is null or empty, otherwise value. |
| _caps | Returns string with first letter in uppercase, optionally converts the rest to lowercase. |
| _trunc | Returns the first length characters of value |
| _round | Returns the value rounded to the specifed number of places |
| _indent | Returns string with spaces at the front, optionally set number, and what string to use as seperator. |
| _undent | Removes extra whitespace characters from a multi-line string |
| _trim | Returns string with whitespace removed rom both the front and end of a string |
| _wrap | Wraps a line value of text at max, using newLine, optionally cutting through words (cutWords). |
pax.template.modifier._def = function( value, defaultValue )
Returns defaultValue, if value is null or empty, otherwise value.
| value | which value to check |
| defaultValue | which value to return, if value is null or empty |
<div id='pax.template.modifier.def.example1'></div>
[:.
var myVars = {
currency: '$',
value: null
};
pax.template.render( "You have [" + ":= currency :" + "][" + ":= _def( value, 0) :" + "] in your pocket.", myVars, pax.$('pax.template.modifier.def.example1') );
:]This ensures you get a default value of 0, even when values are null.
pax.template.modifier._caps = function( value, restLower )
Returns string with first letter in uppercase, optionally converts the rest to lowercase.
| value | which value to use |
| restLower | boolean to decide if we should convert the rest of the string to lowercase |
<div id='pax.template.modifier.capitalise.example1'></div>
[:.
var myVars = { name: 'john' };
pax.template.render( "Hello [" + ":= _caps( name ) :" + "]!", myVars, pax.$('pax.template.modifier.capitalise.example1') );
:]This would show “Hello John!”. Notice that “john” has been capitalised.
<div id='pax.template.modifier.capitalise.example2'></div>
[:.
var myVars = { name: 'joHnAtHaN' };
pax.template.render( "Hello [" + ":= _caps( name, true ) :" + "]!", myVars, pax.$('pax.template.modifier.capitalise.example2') );
:]This would show “Hello Johnathan!”. Notice that “joHnAtHaN” has been capitalised, and lower cased after the first letter.
pax.template.modifier._trunc = function( value, length )
Returns the first length characters of value
| value | which value to use |
| length | how many characters to return |
<div id='pax.template.modifier.truncate.example1'></div>
[:.
var myVars = {
account: '0123456789012',
bsb: 6
};
pax.template.render( "Your BSB is: [" + ":= _trunc( account, bsb) :" + "].", myVars, pax.$('pax.template.modifier.truncate.example1') );
:]This would show “Your BSB is: 012345.”.
pax.template.modifier._round = function( value, places )
Returns the value rounded to the specifed number of places
| value | which value to use |
| placs | how many decimal places to round to |
<div id='pax.template.modifier.round.example1'></div>
[:.
var myVars = {
value: '256.187287',
places: 2
};
pax.template.render( "Rounded value is: [" + ":= _round( value, places ) :" + "]", myVars, pax.$('pax.template.modifier.round.example1') );
:]This would show “Rounded value is: 256.19”.
pax.template.modifier._indent = function( value, number, string )
Returns string with spaces at the front, optionally set number, and what string to use as seperator.
| value | Which value to use |
| number | How many spaces to insert |
| string | What string to use for indentation |
01234567890123456789
<div id='pax.template.modifier.indent.example1'></div>
[:.
var myVars = { fruit: 'apples' };
pax.template.render( "([" + ":= _indent( fruit, 7 ) :" + "])", myVars, pax.$('pax.template.modifier.indent.example1') );
:]This would show “( apples)”, ie: adding 7 spaces before the fruit.
01234567890123456789
<div id='pax.template.modifier.indent.example2'></div>
[:.
var myVars = { fruit: 'apples' };
pax.template.render( "([" + ":= _indent( fruit, 1, 'I like ' ) :" + "])", myVars, pax.$('pax.template.modifier.indent.example2') );
:]This would show “(I like apples)”, ie: adding the specified string 1 time before the fruit.
pax.template.modifier._undent = function( value, tabSpaces )
Removes extra whitespace characters from a multi-line string
| value | The string to use |
| tabSpaces | Optionally specify how many spaces per tab, default = 4 |
var myCode = "
for( var x = 0; x < 10; x ++ ) {\
for( var y = 0; y < 10; y ++ ) {\
result += x * y;\
}\
}\
";
<div id='pax.template.modifier.undent.example1'></div>
[:.
var myVars = { code: myCode };
pax.template.render( "<xmp>[" + ":= _undent( code ) :" + "]</xmp>", myVars, pax.$('pax.template.modifier.undent.example1') );
:]This would show the code in the div, un-indented
pax.template.modifier._trim = function( value )
Returns string with whitespace removed rom both the front and end of a string
| value | Which value to use |
<div id='pax.template.modifier.trim.example1'></div>
[:.
var myVars = { value: ' This is some text that has whitespace befor and after the text ' };
pax.template.render( "([" + ":= _trim( value ) :" + "])", myVars, pax.$('pax.template.modifier.trim.example1') );
:]This would show “(This is some text that has whitespace befor and after the text)”, ie: removing whitespace before and after the text
pax.template.modifier._wrap = function( value, max, newLine, cutWords )
Wraps a line value of text at max, using newLine, optionally cutting through words (cutWords).
| value | the string to wrap |
| max | where to wrap lines at in number of characters |
| newLine | string to use for seperating lines, defaults to “<br>” |
| cutWords | boolean to allow cutting of words, false by default |
<div>0--------10--------20--------30--------40</div>
<div>|123456789|123456789|123456789|123456789|</div>
<div>'----'----'----'----'----'----'----'----'</div>
<div id='pax.template.modifier.wrap.example1'></div>
[:.
var myVars = {
text: 'Hi there, I am a line of text I need my words, such as supercalafragalisticexpialidocious wrapped asap!.',
max: 20
};
pax.template.render( "[" + ":= _wrap( text, max ) :" + "]", myVars, pax.$('pax.template.modifier.wrap.example1') );
:]This would show the line wrapped at 20 chars, without cutting any long words.
<div>0--------10--------20--------30--------40--------50--------60--------70--------80--------90-------100-------110</div>
<div>|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|</div>
<div>'----'----'----'----'----'----'----'----'----'----'----'----'----'----'----'----'----'----'----'----'----'----'</div>
<div id='pax.template.modifier.wrap.example2'></div>
[:.
var myVars = {
text: 'Hi there, I am a line of text I need my words, such as supercalafragalisticexpialidocious wrapped asap!.',
max: 20,
newLine: '|'
};
pax.template.render( "[" + ":= _wrap( text, max, newLine ) :" + "]", myVars, pax.$('pax.template.modifier.wrap.example2') );
:]This would show the line seperated at 20 chars by the specified seperator “|”, without cutting any long words.
<div>0--------10--------20</div>
<div>|123456789|123456789|</div>
<div>'----'----'----'----'</div>
<div id='pax.template.modifier.wrap.example3'></div>
[:.
var myVars = {
text: 'Hi there, I am a line of text I need my words, such as supercalafragalisticexpialidocious wrapped asap!.',
max: 20,
newLine: '<br' + '>',
cutWords: true
};
pax.template.render( "[" + ":= _wrap( text, max, newLine, cutWords ) :" + "]", myVars, pax.$('pax.template.modifier.wrap.example3') );
:]This would show the line wrapped at 20 chars, but with words longer than 20 chars cut.
Returns defaultValue, if value is null or empty, otherwise value.
pax.template.modifier._def = function( value, defaultValue )
Returns string with first letter in uppercase, optionally converts the rest to lowercase.
pax.template.modifier._caps = function( value, restLower )
Returns the first length characters of value
pax.template.modifier._trunc = function( value, length )
Returns the value rounded to the specifed number of places
pax.template.modifier._round = function( value, places )
Returns string with spaces at the front, optionally set number, and what string to use as seperator.
pax.template.modifier._indent = function( value, number, string )
Removes extra whitespace characters from a multi-line string
pax.template.modifier._undent = function( value, tabSpaces )
Returns string with whitespace removed rom both the front and end of a string
pax.template.modifier._trim = function( value )
Wraps a line value of text at max, using newLine, optionally cutting through words (cutWords).
pax.template.modifier._wrap = function( value, max, newLine, cutWords )