pax.template.modifier

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.

Author

Mikkel Bergmann, http://www.pointful.com

Note

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.

Summary
pax.template.modifierThis 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
_defReturns defaultValue, if value is null or empty, otherwise value.
_capsReturns string with first letter in uppercase, optionally converts the rest to lowercase.
_truncReturns the first length characters of value
_roundReturns the value rounded to the specifed number of places
_indentReturns string with spaces at the front, optionally set number, and what string to use as seperator.
_undentRemoves extra whitespace characters from a multi-line string
_trimReturns string with whitespace removed rom both the front and end of a string
_wrapWraps a line value of text at max, using newLine, optionally cutting through words (cutWords).

Functions

_def

pax.template.modifier._def = function(value,
defaultValue)

Returns defaultValue, if value is null or empty, otherwise value.

Parameters

valuewhich value to check
defaultValuewhich value to return, if value is null or empty

Example

<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.template.modifier.def.example1') );
:]

This ensures you get a default value of 0, even when values are null.

_caps

pax.template.modifier._caps = function(value,
restLower)

Returns string with first letter in uppercase, optionally converts the rest to lowercase.

Parameters

valuewhich value to use
restLowerboolean to decide if we should convert the rest of the string to lowercase

Example

<div id='pax.template.modifier.capitalise.example1'></div>
[:.
    var myVars = { name: 'john' };
    pax.template.render( "Hello [" + ":= _caps( name ) :" + "]!", myVars, $('pax.template.modifier.capitalise.example1') );
:]

This would show “Hello John!”.  Notice that “john” has been capitalised.

Example

<div id='pax.template.modifier.capitalise.example2'></div>
[:.
    var myVars = { name: 'joHnAtHaN' };
    pax.template.render( "Hello [" + ":= _caps( name, true ) :" + "]!", myVars, $('pax.template.modifier.capitalise.example2') );
:]

This would show “Hello Johnathan!”.  Notice that “joHnAtHaN” has been capitalised, and lower cased after the first letter.

_trunc

pax.template.modifier._trunc = function(value,
length)

Returns the first length characters of value

Parameters

valuewhich value to use
lengthhow many characters to return

Example

<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.template.modifier.truncate.example1') );
:]

This would show “Your BSB is: 012345.”.

_round

pax.template.modifier._round = function(value,
places)

Returns the value rounded to the specifed number of places

Parameters

valuewhich value to use
placshow many decimal places to round to

Example

<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.template.modifier.round.example1') );
:]

This would show “Rounded value is: 256.19”.

_indent

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.

Parameters

valueWhich value to use
numberHow many spaces to insert
stringWhat string to use for indentation

Example

01234567890123456789
<div id='pax.template.modifier.indent.example1'></div>
[:.
    var myVars = { fruit: 'apples' };
    pax.template.render( "([" + ":= _indent( fruit, 7 ) :" + "])", myVars, $('pax.template.modifier.indent.example1') );
:]

This would show “(&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;apples)”, ie: adding 7 spaces before the fruit.

Example

01234567890123456789
<div id='pax.template.modifier.indent.example2'></div>
[:.
    var myVars = { fruit: 'apples' };
    pax.template.render( "([" + ":= _indent( fruit, 1, 'I like ' ) :" + "])", myVars, $('pax.template.modifier.indent.example2') );
:]

This would show “(I like apples)”, ie: adding the specified string 1 time before the fruit.

_undent

pax.template.modifier._undent = function(value,
tabSpaces)

Removes extra whitespace characters from a multi-line string

Parameters

valueThe string to use
tabSpacesOptionally specify how many spaces per tab, default = 4

Example

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.template.modifier.undent.example1') );
:]

This would show the code in the div, un-indented

_trim

pax.template.modifier._trim = function(value)

Returns string with whitespace removed rom both the front and end of a string

Parameters

valueWhich value to use

Example

<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.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

_wrap

pax.template.modifier._wrap = function(value,
max,
newLine,
cutWords)

Wraps a line value of text at max, using newLine, optionally cutting through words (cutWords).

Parameters

valuethe string to wrap
maxwhere to wrap lines at in number of characters
newLinestring to use for seperating lines, defaults to “<br>”
cutWordsboolean to allow cutting of words, false by default

Example

<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.template.modifier.wrap.example1') );
:]

This would show the line wrapped at 20 chars, without cutting any long words.

Example

<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.template.modifier.wrap.example2') );
:]

This would show the line seperated at 20 chars by the specified seperator “|”, without cutting any long words.

Example

<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.template.modifier.wrap.example3') );
:]

This would show the line wrapped at 20 chars, but with words longer than 20 chars cut.

pax.template.modifier._def = function(value,
defaultValue)
Returns defaultValue, if value is null or empty, otherwise value.
pax.template.modifier._caps = function(value,
restLower)
Returns string with first letter in uppercase, optionally converts the rest to lowercase.
pax.template.modifier._trunc = function(value,
length)
Returns the first length characters of value
pax.template.modifier._round = function(value,
places)
Returns the value rounded to the specifed number of places
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.
pax.template.modifier._undent = function(value,
tabSpaces)
Removes extra whitespace characters from a multi-line string
pax.template.modifier._trim = function(value)
Returns string with whitespace removed rom both the front and end of a string
pax.template.modifier._wrap = function(value,
max,
newLine,
cutWords)
Wraps a line value of text at max, using newLine, optionally cutting through words (cutWords).
Close