underscore.js模板引擎内的模板(递归)运行模板 [英] Run Template in Template (recursion) within underscore.js template engine

查看:140
本文介绍了underscore.js模板引擎内的模板(递归)运行模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Backbone.js的和underscore.js构建JavaScript应用。由于阅读和尝试运行模板类似下面中的模板小时,这是越来越令人沮丧。

I am using backbone.js and underscore.js to build an javascript application. Since hours of reading and trying to run a template within a template like below, it is getting more and more frustrating.

在underscore.js模板引擎使用构建我的模板:

My template using the build in underscore.js template engine:

<script id="navigation_template" type="text/template">
  <div><%= title %>
      <% _.each(children, function(child) { %>
          <% render_this_template_recursively(child) %>
      <% }); %>
  </div>
</script>

我想使这个模板为每个子元素(render_this_template_recursively(子))。

I would like to render this template for every child element ( render_this_template_recursively(child) ).

我怎样才能做到这一点?

How can I do this?

感谢

推荐答案

我没有亲自试过,但_.template返回一个函数(我把它命名为templateFn强调的),所以你可以把它传递到模板是这样的:

I've not personally tried this but _.template returns a function (I've named it templateFn to emphasize that), so you could pass it into the template like this:

var templateFn = _.template($('#navigation_template').html());

$(this.el).html(templateFn({model: this.model, templateFn: templateFn}));

我是传递整个模型的通知(假设你的模式有一个孩子财产本身的骨干机型的集合),你的模板将被更改为:

Notice that i'm passing in the whole model (assuming that your model has a children property which is itself a collection of backbone models) and your template would be changed to:

<script id="navigation_template" type="text/template">
  <div><%= model.escape('title') %>
      <% _.each(model.children, function(child) { %>
          <%= templateFn(child, templateFn) %>
      <% }); %>
  </div>
</script>

祝你好运。我希望这对你的作品

Good luck. I hope this works for you

这篇关于underscore.js模板引擎内的模板(递归)运行模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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