在underscore.js模板每个循环 [英] each loop in underscore.js template

查看:523
本文介绍了在underscore.js模板每个循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里做一些错误的,但我看不到它!我试着去循环的下划线模板的数组。它没有,但这样我失去了一些东西的工作,这是我的code,我的模板,做工精细,否则,它只是_.each东西,这小虫子掉出来:

 <脚本类型=文/模板ID =PageContent>
    < D​​IV CLASS =COL2>
        < @ _.each([0,1,2,3,4],功能(我){@>< P>< @我@>< / P>< @}); @>
    < / DIV>
< / SCRIPT>

我也做了一些模板设置是这样的:

  _。templateSettings = {
    插值:/\\<\\@(.+?)\\@\\>/gim
};


解决方案

由于你只在您的自定义模板设置定义的正则表达式的插值,下划线不知道什么时候,以评估前pressions。当你定义自定义模板设置您需要定义和插值与评价进行区分。从下划线模板()文件:


  

定义一个正则表达式的插值,和(可选)评估正则表达式匹配,应该分别插入和评估,前pressions。如果没有评估正则表达式提供,你的模板将只能够插入值。


在一个标准的(没有自定义设置)模板不同的是评价:&LT;%%&GT; 和值插值:&LT;%= %方式&gt;

因此​​,举例来说,上面的模板应该是(与标准模板设置):

 &LT;%_.each([0,1,2,3,4],功能(我){%GT;&LT; P&GT;&LT;%= I%GT ;&所述; / p&GT;&下;%}); %GT;

如果您希望继续使用自定义设置,您需要在_.templateSettings定义一个正则表达式的评价,以及。根据您的问题和意见是这样的:

  _.templateSettings = {
      插值:/\\<\\@\\=(.+?)\\@\\>/gim,
      评价:/\\<\\@(.+?)\\@\\>/gim
  };

,然后更新您的模板使用约code语言块的评估表和周围值插值形式,像这样:

 &LT;脚本类型=文/模板ID =pageContent&GT;
    &LT; D​​IV CLASS =COL2&GT;
        &LT; @ _.each([0,1,2,3,4],功能(我){@&GT;&LT; P&GT;&LT; @ = I @&GT;&LT; / P&GT;&LT; @}); @&GT;
    &LT; / DIV&GT;
&LT; / SCRIPT&GT;

来源:<一个href=\"http://documentcloud.github.com/underscore/#template\">http://documentcloud.github.com/underscore/#template

I'm doing something wrong here but I can't see it! Im trying to loop an array in a underscore template. It doesn't work though so I'm missing something, Here's my code, my templates work fine otherwise, it's just the _.each stuff that's bugging out:

<script type="text/template" id="PageContent">
    <div class="col2">
        <@ _.each([0,1,2,3,4], function(i) { @>  <p><@ i @></p> <@ }); @>
    </div>    
</script>

I've also done some template settings like this:

_.templateSettings = {
    interpolate: /\<\@(.+?)\@\>/gim
};

解决方案

Because you've only defined an interpolation regex in your custom template settings, underscore doesn't know when to evaluate expressions. When you define custom template settings you need to define and differentiate between interpolation and evaluation. From the underscore template() documentation:

Define an interpolate regex, and an (optional) evaluate regex to match expressions that should be inserted and evaluated, respectively. If no evaluate regex is provided, your templates will only be capable of interpolating values.

In a standard (no custom settings) template the difference is evaluation: <% %> and value interpolation: <%= %>.

So, for example, your template above should be (with standard template settings):

<% _.each([0,1,2,3,4], function(i) { %>  <p><%= i %></p> <% }); %>

If you want to continue using custom settings you'll need to define an evaluation regex in _.templateSettings as well. Based on your questions and comments something like:

   _.templateSettings = {
      interpolate: /\<\@\=(.+?)\@\>/gim,
      evaluate: /\<\@(.+?)\@\>/gim
  };

And then update your template to use the evaluation form around code blocks and the interpolation form around values, like so:

<script type="text/template" id="pageContent">
    <div class="col2">
        <@ _.each([0,1,2,3,4], function(i) { @>  <p><@= i @></p> <@ }); @>
    </div>    
</script>

source: http://documentcloud.github.com/underscore/#template

这篇关于在underscore.js模板每个循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆