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.
getTags()
. Returns an object with all the custom attributes.setTags( tags )
. Sets the custom attributes.useOriginalTags()
. Forces ZPT-JS to use the attributes of ZPT.
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.
getFormatter( id )
. Returns a formatter by id.registerFormatter( id, formatter )
. Register a formatter using an id.unregisterFormatter( id )
. Unregister a formatter by id.
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, ...
getConf()
. Returns an object with all the configuration.setConf( conf )
. Sets theconf
object.
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
getLogger()
. Gets the current logger.setLogger( logger )
. Sets a new logger.
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.
getBooleanAttributes()
. Returns the boolean attributes object.setBooleanAttributes( booleanAttributes )
. Sets a new boolean attributes object.isBooleanAttribute( attribute )
. Returnstrue
is the attribute is boolean,false
otherwise.
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.
getAltAttributes()
. Returns the alt attributes object.setAltAttributes( altAttributes )
. Sets a new alt attributes object.isAltAttribute( attribute )
. Returnstrue
is the attribute is alt,false
otherwise.
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.
setErrorFunction( errorFunction )
. Sets a function that will be invoked if there is an error and no failCallback is set.asyncError( url, error, failCallback )
. Method invoked by ZPT when an asynchronous action fails. Used internally: there is no need to invoke it explicitly by users of ZPT-JS.
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.
repeat( index, length, offset )
. Returns a new instance ofLoopItem
.
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.
getFolderDictionaries()
. Returns the folder dictionaries.setFolderDictionaries( folderDictionaries )
. Sets the folder dictionaries.
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:
- Using a data-declare attribute. All the variables used in this node must be declared.
- Using
zpt.context.setStrictMode( true )
. All the variables used in the root node must be declared.
The methods related to Strict mode (defined in context) are:
setStrictMode( strictMode )
. Set the strict mode of the node root totrue
orfalse
.isStrictMode()
. Returns the current value ofstrictMode
.