X-Git-Url: http://git.ithinksw.org/extjs.git/blobdiff_plain/6746dc89c47ed01b165cc1152533605f97eb8e8d..HEAD:/src/XTemplate.js diff --git a/src/XTemplate.js b/src/XTemplate.js index c0c16cba..9ec15949 100644 --- a/src/XTemplate.js +++ b/src/XTemplate.js @@ -13,284 +13,236 @@ If you are unsure which license is appropriate for your use, please contact the */ /** - * @class Ext.XTemplate - * @extends Ext.Template - *

A template class that supports advanced functionality like:

- *

XTemplate provides the templating mechanism built into:

- * - * The {@link Ext.Template} describes - * the acceptable parameters to pass to the constructor. The following - * examples demonstrate all of the supported features.

- * - *
+ * var tpl = new Ext.XTemplate( + * '

Name: {name}

', + * '

Kids: ', + * '', + * '', // <-- Note that the > is encoded + * '

{#}: {name}

', // <-- Auto-number each item + * '

In 5 Years: {age+5}

', // <-- Basic math + * '

Dad: {parent.name}

', + * '', + * '

' + * ); + * tpl.overwrite(panel.body, data); + * + * # Execute arbitrary inline code with special built-in template variables + * + * Anything between `{[ ... ]}` is considered code to be executed in the scope of the template. There are some special + * variables available in that code: + * + * - **values**: The values in the current scope. If you are using scope changing sub-templates, + * you can change what values is. + * - **parent**: The scope (values) of the ancestor template. + * - **xindex**: If you are in a looping template, the index of the loop you are in (1-based). + * - **xcount**: If you are in a looping template, the total length of the array you are looping. + * + * This example demonstrates basic row striping using an inline code block and the xindex variable: + * + * var tpl = new Ext.XTemplate( + * '

Name: {name}

', + * '

Company: {[values.company.toUpperCase() + ", " + values.title]}

', + * '

Kids: ', + * '', + * '

', + * '{name}', + * '
', + * '

' + * ); + * tpl.overwrite(panel.body, data); + * + * # Template member functions * - * @param {Mixed} config + * One or more member functions can be specified in a configuration object passed into the XTemplate constructor for + * more complex processing: + * + * var tpl = new Ext.XTemplate( + * '

Name: {name}

', + * '

Kids: ', + * '', + * '', + * '

Girl: {name} - {age}

', + * '', + * // use opposite if statement to simulate 'else' processing: + * '', + * '

Boy: {name} - {age}

', + * '
', + * '', + * '

{name} is a baby!

', + * '
', + * '

', + * { + * // XTemplate configuration: + * disableFormats: true, + * // member functions: + * isGirl: function(name){ + * return name == 'Sara Grace'; + * }, + * isBaby: function(age){ + * return age < 1; + * } + * } + * ); + * tpl.overwrite(panel.body, data); */ - Ext.define('Ext.XTemplate', { /* Begin Definitions */ extend: 'Ext.Template', - statics: { - /** - * Creates a template from the passed element's value (display:none textarea, preferred) or innerHTML. - * @param {String/HTMLElement} el A DOM element or its id - * @return {Ext.Template} The created template - * @static - */ - from: function(el, config) { - el = Ext.getDom(el); - return new this(el.value || el.innerHTML, config || {}); - } - }, - /* End Definitions */ argsRe: /]*>((?:(?=([^<]+))\2|<(?!tpl\b[^>]*>))*?)<\/tpl>/, @@ -370,11 +322,18 @@ Ext.define('Ext.XTemplate', { var me = this, t = me.tpls[id]; return t.compiled.call(me, values, parent, xindex, xcount); }, + /** - * @cfg {RegExp} codeRe The regular expression used to match code variables (default: matches {[expression]}). + * @cfg {RegExp} codeRe + * The regular expression used to match code variables. Default: matches {[expression]}. */ codeRe: /\{\[((?:\\\]|.|\n)*?)\]\}/g, + /** + * @cfg {Boolean} compiled + * Only applies to {@link Ext.Template}, XTemplates are compiled automatically. + */ + re: /\{([\w-\.\#]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?(\s?[\+\-\*\/]\s?[\d\.\+\-\*\/\(\)]+)?\}/g, // @private @@ -482,31 +441,20 @@ Ext.define('Ext.XTemplate', { return this; }, - /** - * Returns an HTML fragment of this template with the specified values applied. - * @param {Object} values The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'}) - * @return {String} The HTML fragment - */ + // inherit docs from Ext.Template applyTemplate: function(values) { return this.master.compiled.call(this, values, {}, 1, 1); }, /** - * Compile the template to a function for optimized performance. Recommended if the template will be used frequently. - * @return {Function} The compiled function + * Does nothing. XTemplates are compiled automatically, so this function simply returns this. + * @return {Ext.XTemplate} this */ compile: function() { return this; } }, function() { - /** - * Alias for {@link #applyTemplate} - * Returns an HTML fragment of this template with the specified values applied. - * @param {Object/Array} values The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'}) - * @return {String} The HTML fragment - * @member Ext.XTemplate - * @method apply - */ + // re-create the alias, inheriting it from Ext.Template doesn't work as intended. this.createAlias('apply', 'applyTemplate'); });