## template
+ [link](./template "Link to this entry.")
+ [source](https://github.com/lodash/lodash/blob/4.5.0正式版/lodash.src.js#L12648 "View in source.")
+ [npm](https://www.npmjs.com/package/lodash.template "See the npm package.")
```
_.template([string=''], [options])
```
创建一个预编译模板方法,可以插入数据到模板中 "interpolate" 分隔符相应的位置。 HTML会在 "escape" 分隔符中转换为相应实体。 在 "evaluate" 分隔符中允许执行JavaScript代码。 在模板中可以自由访问变量。 如果设置了选项对象,则会优先覆盖 `_.templateSettings` 的值。
**注意:** 在开发过程中可以使用 [sourceURLs](http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/#toc-sourceurl) 便于调试。
了解更多预编译模板的信息查看 [lodash的自定义构建文档](https://lodash.com/custom-builds)
了解更多 Chrome 沙箱扩展的信息查看 [Chrome的扩展文档](https://developer.chrome.com/extensions/sandboxingEval)
### 参数
1. [string=''] (string)
模板字符串
2. [options] (Object)
选项对象
3. [options.escape] (RegExp)
"escape" 分隔符
4. [options.evaluate] (RegExp)
"evaluate" 分隔符
5. [options.imports] (Object)
导入对象到模板中作为自由变量
6. [options.interpolate] (RegExp)
"interpolate" 分隔符
7. [options.sourceURL] (string)
模板编译的来源URL
8. [options.variable] (string)
数据对象的变量名
### 返回值 (Function)
返回编译模板函数
### 示例
```
// 使用 "interpolate" 分隔符创建编译模板
var compiled = _.template('hello <%= user %>!');
compiled({ 'user': 'fred' });
// => 'hello fred!'
// 使用 HTML "escape" 转义数据的值
var compiled = _.template('<b><%- value %></b>');
compiled({ 'value': '<script>' });
// => '<b><script></b>'
// 使用 "evaluate" 分隔符执行 JavaScript 和 生成HTML代码
var compiled = _.template('<% _.forEach(users, function(user) { %><li><%- user %></li><% }); %>');
compiled({ 'users': ['fred', 'barney'] });
// => '<li>fred</li><li>barney</li>'
// 在 "evaluate" 分隔符中使用内部的 `print` 函数
var compiled = _.template('<% print("hello " + user); %>!');
compiled({ 'user': 'barney' });
// => 'hello barney!'
// 使用 ES 分隔符代替默认的 "interpolate" 分隔符
var compiled = _.template('hello ${ user }!');
compiled({ 'user': 'pebbles' });
// => 'hello pebbles!'
// 使用自定义的模板分隔符
_.templateSettings.interpolate = /{{([\s\S]+?)}}/g;
var compiled = _.template('hello {{ user }}!');
compiled({ 'user': 'mustache' });
// => 'hello mustache!'
// 使用反斜杠符号作为纯文本处理
var compiled = _.template('<%= "\\<%- value %\\>" %>');
compiled({ 'value': 'ignored' });
// => '<%- value %>'
// 使用 `imports` 选项导入 `jq` 作为 `jQuery` 的别名
var text = '<% jq.each(users, function(user) { %><li><%- user %></li><% }); %>';
var compiled = _.template(text, { 'imports': { 'jq': jQuery } });
compiled({ 'users': ['fred', 'barney'] });
// => '<li>fred</li><li>barney</li>'
// 使用 `sourceURL` 选项指定模板的来源URL
var compiled = _.template('hello <%= user %>!', { 'sourceURL': '/basic/greeting.jst' });
compiled(data);
// => 在开发工具的 Sources 选项卡 或 Resources 面板中找到 "greeting.jst"
// 使用 `variable` 选项确保在编译模板中不声明变量
var compiled = _.template('hi <%= data.user %>!', { 'variable': 'data' });
compiled.source;
// => function(data) {
// var __t, __p = '';
// __p += 'hi ' + ((__t = ( data.user )) == null ? '' : __t) + '!';
// return __p;
// }
// 使用 `source` 特性内联编译模板
// 便以查看行号、错误信息、堆栈
fs.writeFileSync(path.join(cwd, 'jst.js'), '\
var JST = {\
"main": ' + _.template(mainText).source + '\
};\
');
```
- 主页
- 入门指南
- 下载
- 引入
- 模块格式
- 深入了解
- 兼容性
- 版本定制
- Array
- chunk
- compact
- concat
- difference
- differenceBy
- differenceWith
- drop
- dropRight
- dropRightWhile
- dropWhile
- fill
- findIndex
- findLastIndex
- flatten
- flattenDeep
- flattenDepth
- fromPairs
- head first
- indexOf
- initial
- intersection
- intersectionBy
- intersectionWith
- join
- last
- lastIndexOf
- prototype.reverse
- pull
- pullAll
- pullAllBy
- pullAt
- remove
- slice
- sortedIndex
- sortedIndexBy
- sortedIndexOf
- sortedLastIndex
- sortedLastIndexBy
- sortedLastIndexOf
- sortedUniq
- sortedUniqBy
- tail
- take
- takeRight
- takeRightWhile
- takeWhile
- union
- unionBy
- unionWith
- uniq
- uniqBy
- uniqWith
- unzip
- unzipWith
- without
- xor
- xorBy
- xorWith
- zip
- zipObject
- zipObjectDeep
- zipWith
- Collection
- countBy
- every
- filter
- find
- findLast
- flatMap
- forEach each
- forEachRight eachRight
- groupBy
- includes
- invokeMap
- keyBy
- map
- orderBy
- partition
- reduce
- reduceRight
- reject
- sample
- sampleSize
- shuffle
- size
- some
- sortBy
- Date
- now
- Function
- after
- ary
- before
- bind
- bindKey
- curry
- curryRight
- debounce
- defer
- delay
- flip
- memoize
- negate
- once
- overArgs
- partial
- partialRight
- rearg
- rest
- spread
- throttle
- unary
- wrap
- Lang
- castArray
- clone
- cloneDeep
- cloneDeepWith
- cloneWith
- eq
- gt
- gte
- isArguments
- isArray
- isArrayBuffer
- isArrayLike
- isArrayLikeObject
- isBoolean
- isBuffer
- isDate
- isElement
- isEmpty
- isEqual
- isEqualWith
- isError
- isFinite
- isFunction
- isInteger
- isLength
- isMap
- isMatch
- isMatchWith
- isNaN
- isNative
- isNil
- isNull
- isNumber
- isObject
- isObjectLike
- isPlainObject
- isRegExp
- isSafeInteger
- isSet
- isString
- isSymbol
- isTypedArray
- isUndefined
- isWeakMap
- isWeakSet
- lt
- lte
- toArray
- toInteger
- toLength
- toNumber
- toPlainObject
- toSafeInteger
- toString
- Math
- add
- ceil
- floor
- max
- maxBy
- mean
- min
- minBy
- round
- subtract
- sum
- sumBy
- Methods
- templateSettings.imports._
- Number
- clamp
- inRange
- random
- Object
- assign
- assignIn extend
- assignInWith extendWith
- assignWith
- at
- create
- defaults
- defaultsDeep
- findKey
- findLastKey
- forIn
- forInRight
- forOwn
- forOwnRight
- functions
- functionsIn
- get
- has
- hasIn
- invert
- invertBy
- invoke
- keys
- keysIn
- mapKeys
- mapValues
- merge
- mergeWith
- omit
- omitBy
- pick
- pickBy
- result
- set
- setWith
- toPairs
- toPairsIn
- transform
- unset
- values
- valuesIn
- Properties
- templateSettings
- templateSettings.escape
- templateSettings.evaluate
- templateSettings.imports
- templateSettings.interpolate
- templateSettings.variable
- VERSION
- Seq
- _
- chain
- prototype.at
- prototype.chain
- prototype.commit
- prototype.next
- prototype.plant
- prototype.Symbol.iterator
- prototype.value run, toJSON, valueOf
- tap
- thru
- wrapperFlatMap
- String
- camelCase
- capitalize
- deburr
- endsWith
- escape
- escapeRegExp
- kebabCase
- lowerCase
- lowerFirst
- pad
- padEnd
- padStart
- parseInt
- repeat
- replace
- snakeCase
- split
- startCase
- startsWith
- template
- toLower
- toUpper
- trim
- trimEnd
- trimStart
- truncate
- unescape
- upperCase
- upperFirst
- words
- Util
- attempt
- bindAll
- cond
- conforms
- constant
- flow
- flowRight
- identity
- iteratee
- matches
- matchesProperty
- method
- methodOf
- mixin
- noConflict
- noop
- nthArg
- over
- overEvery
- overSome
- property
- propertyOf
- range
- rangeRight
- runInContext
- times
- toPath
- uniqueId