added _.template definition

This commit is contained in:
Brian Zengel
2013-10-20 19:49:01 -04:00
parent ad30e9d356
commit 39b96f597c
2 changed files with 68 additions and 35 deletions
+16 -14
View File
@@ -745,6 +745,21 @@ result = <any>_.result(object, 'stuff');
var tempObject = {};
result = <typeof _>_.runInContext(tempObject);
result = <_.TemplateExecutor>_.template('hello <%= name %>');
result = <string>_.template('<b><%- value %></b>', { 'value': '<script>' });
var listTemplate = '<% _.forEach(people, function(name) { %><li><%- name %></li><% }); %>';
result = <string>_.template(listTemplate, { 'people': ['moe', 'larry'] });
result = <string>_.template('hello ${ name }', { 'name': 'curly' });
result = <string>_.template('<% print("hello " + name); %>!', { 'name': 'larry' });
var listTemplate = '<% $.each(people, function(name) { %><li><%- name %></li><% }); %>';
result = <string>_.template(listTemplate, { 'people': ['moe', 'larry'] }, { 'imports': { '$': jQuery } });
result = <_.TemplateExecutor>_.template('hello <%= name %>', null, { 'sourceURL': '/basic/greeting.jst' });
result = <_.TemplateExecutor>_.template('hi <%= data.name %>!', null, { 'variable': 'data' });
result = <string>(<_.TemplateExecutor>result).source;
//////////////////////////////////////////////////////////////////////////////////////////////////
var iceCream = { flavor: "chocolate" };
@@ -794,20 +809,7 @@ _.result(object, 'cheese');
_.result(object, 'stuff');
var compiled = _.template("hello: <%= name %>");
compiled({ name: 'moe' });
var list2 = "<% _.each(people, function(name) { %> <li><%= name %></li> <% }); %>";
_.template(list2, { people: ['moe', 'curly', 'larry'] });
var template = _.template("<b><%- value %></b>");
template({ value: '<script>' });
var compiled2 = _.template("<% print('Hello ' + epithet); %>");
compiled2({ epithet: "stooge" });
_.templateSettings = {
interpolate: /\{\{(.+?)\}\}/g
};
var template2 = _.template("Hello {{ name }}!");
template2({ name: "Mustache" });
_.template("Using 'with': <%= data.answer %>", { answer: 'no' }, { variable: 'data' });
_(['test', 'test']).pick(['test2', 'test2']);
+52 -21
View File
@@ -57,19 +57,29 @@ declare module _ {
**/
interface TemplateSettings {
/**
* Default value is '/<%([\s\S]+?)%>/g'.
* The "escape" delimiter.
**/
escape?: RegExp;
/**
* The "evaluate" delimiter.
**/
evaluate?: RegExp;
/**
* Default value is '/<%=([\s\S]+?)%>/g'.
* An object to import into the template as local variables.
**/
interpolate?: RegExp;
imports?: Dictionary<any>;
/**
* Default value is '/<%-([\s\S]+?)%>/g'.
* The "interpolate" delimiter.
**/
escape?: RegExp;
interpolate?: RegExp;
}
interface TemplateExecutor {
(...data: any[]): string;
source: string;
}
interface ListIterator<T, TResult> {
@@ -2779,7 +2789,43 @@ declare module _ {
**/
export function runInContext(context: any): typeof _;
/**
* A micro-templating method that handles arbitrary delimiters, preserves whitespace, and
* correctly escapes quotes within interpolated code.
*
* Note: In the development build, _.template utilizes sourceURLs for easier debugging. See
* http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/#toc-sourceurl
*
* For more information on precompiling templates see:
* http://lodash.com/#custom-builds
*
* For more information on Chrome extension sandboxes see:
* http://developer.chrome.com/stable/extensions/sandboxingEval.html
* @param text The template text.
* @param data The data object used to populate the text.
* @param options The options object.
* @param options.escape The "escape" delimiter.
* @param options.evaluate The "evaluate" delimiter.
* @param options.import An object to import into the template as local variables.
* @param options.interpolate The "interpolate" delimiter.
* @param sourceURL The sourceURL of the templates compiled source.
* @param variable The data object variable name.
* @return Returns the compiled Lo-Dash HTML template or a TemplateExecutor if no data is passed.
**/
export function template(
text: string): TemplateExecutor;
/**
* @see _.template
**/
export function template(
text: string,
data: any,
options?: TemplateSettings,
sourceURL?: string,
variable?: string): any /* string or TemplateExecutor*/;
@@ -2809,22 +2855,7 @@ declare module _ {
/**
* Compiles JavaScript templates into functions that can be evaluated for rendering. Useful
* for rendering complicated bits of HTML from JSON data sources. Template functions can both
* interpolate variables, using <%= ... %>, as well as execute arbitrary JavaScript code, with
* <% ... %>. If you wish to interpolate a value, and have it be HTML-escaped, use <%- ... %> When
* you evaluate a template function, pass in a data object that has properties corresponding to
* the template's free variables. If you're writing a one-off, you can pass the data object as
* the second parameter to template in order to render immediately instead of returning a template
* function. The settings argument should be a hash containing any _.templateSettings that should
* be overridden.
* @param templateString Lo-Dash HTML template.
* @param data Data to use when compiling `templateString`.
* @param settings Settings to use while compiling.
* @return Returns the compiled Lo-Dash HTML template.
**/
export function template(templateString: string, data?: any, settings?: TemplateSettings): (...data: any[]) => string;
/**
* By default, Lo-Dash uses ERB-style template delimiters, change the