mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-07 10:07:48 +00:00
Made _.template delimeters customizable
This commit is contained in:
14
index.html
14
index.html
@@ -994,6 +994,11 @@ _.uniqueId('contact_');
|
||||
variables. If you're writing a one-off, you can pass the <b>context</b>
|
||||
object as the second parameter to <b>template</b> in order to render
|
||||
immediately instead of returning a template function.
|
||||
<br />
|
||||
If the <i><% … %></i> syntax is not convenient because
|
||||
your templating languages assigns special meaning to it, the delimeters
|
||||
can be customized by passing an object as the first argument. See the
|
||||
code sample below for options.
|
||||
</p>
|
||||
<pre>
|
||||
var compiled = _.template("hello: <%= name %>");
|
||||
@@ -1003,6 +1008,15 @@ compiled({name : 'moe'});
|
||||
var list = "<% _.each(people, function(name) { %> <li><%= name %></li> <% }); %>";
|
||||
_.template(list, {people : ['moe', 'curly', 'larry']});
|
||||
=> "<li>moe</li><li>curly</li><li>larry</li>"
|
||||
|
||||
var custom = "{{ _.each(people, function(name) { }} <li>{{= name }}</li> {{ }); }}";
|
||||
_.template({
|
||||
template: custom,
|
||||
start: '{{', // the code start delimeter
|
||||
end: '}}', // the code end delimeter
|
||||
interpolate: /\{\{=(.+?)\}\}/g // a regex with 1 capture group for the var name
|
||||
}, {people : ['moe', 'curly', 'larry']});
|
||||
=> "<li>moe</li><li>curly</li><li>larry</li>"
|
||||
</pre>
|
||||
|
||||
<h2>Chaining</h2>
|
||||
|
||||
Reference in New Issue
Block a user