ZPT-JS reference - Context

Description

The context groups some important general settings. It makes it easy to customize some settings.

Tags

Some settings related to the custom attributes used by ZPT-JS.

The data-* attributes is used to store custom data private inside the HTML document. These are the standard in HTML5. If you prefer to use the original ZPT's attributes use zpt.context.useOriginalTags() method. You can also use your custom attributes.

Removing first character of attributes:

"use strict";

var zpt = require( 'zpt' );

zpt.context.setTags({
        talCondition:     "data-ondition",
        talRepeat:        "data-epeat",
        talAttributes:    "data-ttributes",
        talContent:       "data-ontent",
        talDefine:        "data-efine",
        talAutoDefine:    "data-uto-define",
        talOmitTag:       "data-mit-tag",
        talReplace:       "data-eplace",
        talOnError:       "data-n-error",
        talDeclare:       "data-eclare",
        metalDefineMacro: "data-efine-macro",
        metalUseMacro:    "data-se-macro",
        metalDefineSlot:  "data-efine-slot",
        metalFillSlot:    "data-ill-slot",
        metalMacro:       "data-acro",
        i18nDomain:       "data-omain",
        i18nLanguage:     "data-anguage",
        rootKey:          "data-oot-key",
        qdup:             "data-dup"
});

[ your zpt invokation here ]
                

Using attributes of ZPT:

"use strict";

var zpt = require( 'zpt' );

zpt.context.useOriginalTags();

[ your zpt invokation here ]
                

... that is equivalent to do:

"use strict";

var zpt = require( 'zpt' );

zpt.context.setTags({
        talCondition:     "tal:condition",
        talRepeat:        "tal:repeat",
        talAttributes:    "tal:attributes",
        talContent:       "tal:content",
        talDefine:        "tal:define",
        talAutoDefine:    "tal:auto-define",
        talOmitTag:       "tal:omit-tag",
        talReplace:       "tal:replace",
        talOnError:       "tal:on-error",
        talDeclare:       "tal:declare",
        metalDefineMacro: "metal:define-macro",
        metalUseMacro:    "metal:use-macro",
        metalDefineSlot:  "metal:define-slot",
        metalFillSlot:    "metal:fill-slot",
        metalMacro:       "data-mmacro",
        i18nDomain:       "i18n:domain",
        i18nLanguage:     "i18n:language",
        rootKey:          "data-root-key",
        qdup:             "data-qdup"
});

[ your zpt invokation here ]
                

An example of template using the original ZPT's attributes:

original-tags-sample.html
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Example using ZPT's attributes</title>

        <script src="original-tags-sample.js" defer></script>
    </head>
    <body>
        <p tal:content="user/name">xxx</p>
    </body>
</html>
    

Formatters

Support of getting and registering formatters.

Registering myCustomFormatter:

"use strict";

var zpt = require( 'zpt' );

zpt.context.registerFormatter( 
    'myCustomFormatter', 
    function( value ){
        return "$" + value;
    }
);

[ your zpt invokation here ]
                

Conf

The conf object includes some key/value pairs related to low level details about ZPT-JS, such as delimiters, built-in variable names, expression ids, ...

WARN: Be careful with this, you can break ZPT-JS!

Getting the value of conf object:

"use strict";

var zpt = require( 'zpt' );

var conf = zpt.context.getConf();
                

The default value of conf object is:

var conf = {
    pathDelimiter:          '|',
    pathSegmentDelimiter:   '/',
    expressionDelimiter:    ' ',
    intervalDelimiter:      ':',
    propertyDelimiter:      '/',
    defineDelimiter:        ';',
    inDefineDelimiter:      ' ',
    attributeDelimiter:     ';',
    inAttributeDelimiter:   ' ',
    domainDelimiter:        ' ',
    i18nOptionsDelimiter:   ';',
    inI18nOptionsDelimiter: ' ',
    argumentsDelimiter:     ',',
    macroDelimiter:         '@',
    declareDelimiter:         ';',
    inDeclareDelimiter:       ' ',

    htmlStructureExpressionPrefix:  "structure",
    globalVariableExpressionPrefix: "global",
    nocallVariableExpressionPrefix: "nocall",

    templateErrorVarName:    "error",
    onErrorVarName:          "on-error",
    onErrorStructureVarName: "on-error-structure",
    i18nDomainVarName:       "i18nDomain",
    i18nLanguageVarName:     "i18nLanguage",
    externalMacroUrlVarName: "externalMacroUrl",
    strictModeVarName:       "strictMode",
    declaredVarsVarName:     "declaredVars",

    repeatVarName:           "repeat",
    windowVarName:           "window",
    contextVarName:          "context",
    nothingVarName:          "nothing",
    defaultVarName:          "default",
    i18nConfResourceId:      "/CONF/",

    nothingVarValue:         "___nothing___",
    defaultVarValue:         "___default___",

    loggingOn: false,
    loggingLevel: log4javascript.Level.ERROR,

    externalMacroPrefixURL: '',
    variableNameRE:         /^[A-Za-z0-9_/-]+$/,
    expressionCacheOn: true,
    attributeCacheOn: true,

    expressionSuffix: EXPRESSION_SUFFIX,
    stringExpression: "string" + EXPRESSION_SUFFIX,
    existsExpression: "exists" + EXPRESSION_SUFFIX,
    notExpression: "not" + EXPRESSION_SUFFIX,
    javaScriptExpression: "js" + EXPRESSION_SUFFIX,
    equalsExpression: "eq" + EXPRESSION_SUFFIX,
    greaterExpression: "gt" + EXPRESSION_SUFFIX,
    lowerExpression: "lt" + EXPRESSION_SUFFIX,
    addExpression: "+" + EXPRESSION_SUFFIX,
    subExpression: "-" + EXPRESSION_SUFFIX,
    mulExpression: "*" + EXPRESSION_SUFFIX,
    divExpression: "/" + EXPRESSION_SUFFIX,
    modExpression: "%" + EXPRESSION_SUFFIX,
    orExpression: "or" + EXPRESSION_SUFFIX,
    andExpression: "and" + EXPRESSION_SUFFIX,
    condExpression: "cond" + EXPRESSION_SUFFIX,
    formatExpression: "format" + EXPRESSION_SUFFIX,
    trExpression: "tr" + EXPRESSION_SUFFIX,
    trNumberExpression: "trNumber" + EXPRESSION_SUFFIX,
    trCurrencyExpression: "trCurrency" + EXPRESSION_SUFFIX,
    trDateTimeExpression: "trDate" + EXPRESSION_SUFFIX,
    inExpression: "in" + EXPRESSION_SUFFIX,
    pathExpression: "",
    jqueryExpression: "$"
}
                

Loggers

Boolean attributes

The presence of a boolean attribute on an element represents the true value, and the absence of the attribute represents the false value. The booleanAttributes is an object with the boolean attributes as keys and 1 as values.

WARN: Be careful with this, you can break ZPT-JS!

The default value of the boolean attributes object is:

var booleanAttributes = {
    "checked" : 1,
    "compact" : 1,
    "declare" : 1,
    "defer" : 1,
    "disabled" : 1,
    "ismap" : 1,
    "multiple" : 1,
    "nohref" : 1,
    "noresize" : 1,
    "noshade" : 1,
    "nowrap" : 1,
    "readonly" : 1,
    "selected" : 1
}
                

Alt attributes

Attributes which don't support setAttribute(). The altAttributes is an object with the alt attributes as keys and 1 as values.

WARN: Be careful with this, you can break ZPT-JS!

The default value of the alt attributes object is:

var altAttributes = {
    "className" : 1,
    "class" : 1,
    "href" : 1,
    "htmlFor" : 1,
    "id" : 1,
    "innerHTML" : 1,
    "label" : 1,
    "style" : 1,
    "src" : 1,
    "text" : 1,
    "title" : 1,
    "value" : 1
}
                

Errors

Some methods about errors and their customization.

Setting an error function to count errors using alert to show them:

"use strict";

var zpt = require( 'zpt' );

var errorCounter = 0;
zpt.context.setErrorFunction(
    function( error ){
        ++errorCounter;
        var msg = Array.isArray( error )?
            error.join( '\n' ):
            error;
        alert( 'Another error: ' + msg );
    }
);

[ your zpt invokation here ]
                

Repeat

The repeat method is used by ZPT-JS internally to implement loops using data-repeat attributes. There is no need to invoke it explicitly by users of ZPT-JS.

Folder dictionaries

Folder dictionaries is an array of objects preloaded by ZPT-JS at the first invokation of ZPT. Used internally: there is no need to invoke it explicitly by users of ZPT-JS.

Strict mode

Strict mode makes it easy to force to declare variables. If strict mode is set to true and ZPT-JS finds a not declared variable an error occurs. Strict mode can be defined in two ways:

The methods related to Strict mode (defined in context) are: