最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What are the settings accepted by the underscore templates? - Stack Overflow

programmeradmin1浏览0评论

The _.template() function accepts settings as a third argument, allowing you to change a few things about how do the templates work, including making the templates more Moustache-like. But is this all the settings can do? Can you provide a full list of keys and their meanings for the settings object? And is it possible to pile settings into a template (since the data argument goes before settings, it seems that providing settings along with a template would result in underscore trying to apply a template immediately, assuming settings to be the data).

The _.template() function accepts settings as a third argument, allowing you to change a few things about how do the templates work, including making the templates more Moustache-like. But is this all the settings can do? Can you provide a full list of keys and their meanings for the settings object? And is it possible to pile settings into a template (since the data argument goes before settings, it seems that providing settings along with a template would result in underscore trying to apply a template immediately, assuming settings to be the data).

Share Improve this question asked Oct 14, 2013 at 12:28 SeptagramSeptagram 9,79513 gold badges56 silver badges82 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

But is this all the settings can do?

Yes, all possible settings are mentioned in the docs. You can read the annoted source as well.

Can you provide a full list of keys and their meanings for the settings object?

  • interpolate: regex to match expressions that should be interpolated verbatim
  • escape: regex to match expressions that should be inserted after being HTML escaped
  • evaluate: regex to match expressions that should be evaluated without insertion into the resulting string.
  • variable: A variable name to access the data as properties, instead of using a with statement

And is it possible to pile settings into a template?

Yes. Simply pass any falsy value (null, undefined, false, …) for data and the method will return a template function instead of rendering it right away.

If you check out the annotated source (I highly remend it, there's a lot of great explanations there), the three options for settings are evaluate, interpolate and escape. Here's a link to the templateSettings object:

http://underscorejs/docs/underscore.html#section-131

I'm not sure I fully understand the question about piling settings into a template, but you can set global template settings (so you don't have to pass it in as an argument to the template method, like this:

_.templateSettings = {
      interpolate: /<%=([\s\S]+?)%>/g,
      evaluate: /<%([\s\S]+?)%>/g
  };

Sorry if that didn't answer your question, but hopefully the annotated source will shed some light.

发布评论

评论列表(0)

  1. 暂无评论