我应该将 Backbone 模板放置在 Rails 框架中的哪个位置?

发布于 2024-12-15 18:58:56 字数 178 浏览 0 评论 0原文

我是一名试图学习 Backbone 的 Rails 开发人员,然后遇到了这个问题:由于 Underscore 模板包含像 <%=%> 这样的符号,我猜模板可以不会包含在 erb 文件中,那么每个模板都有一个 Rails 部分可以吗?它应该是什么扩展名?

I'm a rails developer trying to learn Backbone and then I ran into this problem: since Underscore templates include symbols like <%=%>, I guess templates can't be included into erb files, so is it okay to have a rails partial for every single template? And what extension should it be?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云胡 2024-12-22 18:58:56

您可以通过在开始标记中使用两个 % 来转义 erb 符号,并将主干模板放入 Rails 视图中:

<script type='text/template' id="my-template'>
  <%%= name %>
</script>

将在页面中输出以下内容:

<script type='text/template' id="my-template'>
  <%= name %>
</script>

将 Backbone 模板直接放入 Rails 视图中是恕我直言,当您尝试学习时,这是最好的选择。您已经在努力应对新概念,无需添加其他障碍。

You can escape the erb symbols by using two % in the opening tag, and put your backbone templates in the rails views:

<script type='text/template' id="my-template'>
  <%%= name %>
</script>

will output the following in your page:

<script type='text/template' id="my-template'>
  <%= name %>
</script>

Putting your Backbone templates directly in your rails views is IMHO the best option when you're trying to learn. You're already wrestling with the new concepts, no need to add another hurdle.

公布 2024-12-22 18:58:56

从 Rails 3.1 开始,它提供了两个使使用 Backbone 模板变得更容易的功能:资源管道和自动 JST(JavaScript 模板)编译。

app/assets 文件夹中创建一个名为 templates 的目录。该目录将被资产管道自动选取。

接下来,使用扩展名 jst 和您要创建的模板类型 ejs(嵌入式 JavaScript)来命名该目录中的文件。您甚至可以将它们嵌套在目录中。例如:

app/assets/templates/my_template.jst.ejs
app/assets/templates/bookmarks/show.jst.ejs

资产管道还允许您通过简单地更改文件扩展名(并包括任何必要的 gems)来使用其他模板语言,例如嵌入式咖啡脚本、mustache、handlebars 等。

现在,要在 Backbone 视图中引用 JST 模板,只需使用文件名的路径:

var Bookmark = Backbone.View.extend({
  template: JST['bookmarks/show'],
  render: function() {
    this.$el.html(this.template(this.model.attributes));
    return this;
  }
});

您可能需要将此行添加到您的 application.js 中:

// require_tree ../templates

这是一篇很好的文章,它在更多细节: http://www.bigjason.com/blog/precompiled-javascript-templates-rails-3-1

Starting with Rails 3.1, it provides two things that make working with Backbone templates a little easier: the asset pipeline, and automatic JST (JavaScript Template) compilation.

Create a directory in your app/assets folder called templates. This directory will automatically be picked up by the asset pipeline.

Next, name the files in that directory with an extension of jst and the type of template you are creating ejs (embedded javascript). You can even nest them in directories. For example:

app/assets/templates/my_template.jst.ejs
app/assets/templates/bookmarks/show.jst.ejs

The asset pipeline also allows you to use other templating languages like embedded coffeescript, mustache, handlebars, etc. by simply changing the file extension (and including any necessary gems).

Now to reference your JST templates in your Backbone views, simply use the path to the filename:

var Bookmark = Backbone.View.extend({
  template: JST['bookmarks/show'],
  render: function() {
    this.$el.html(this.template(this.model.attributes));
    return this;
  }
});

You may need to add this line to your application.js:

// require_tree ../templates

Here's a nice article which explains all of this in a little more detail: http://www.bigjason.com/blog/precompiled-javascript-templates-rails-3-1

枕梦 2024-12-22 18:58:56

您应该将 Backbone 模板放在哪里?我想说无处可去。我相信在大多数 Rails 应用程序中,服务器应该负责所有 HTML 的渲染,而客户端 JavaScript 应该只负责将渲染的 HTML 插入到 DOM 中。除此之外,这使得国际化变得更加容易。

例外情况是,如果 Rails 只是用作主要在客户端运行的应用程序的轻量级后端(尽管在这种情况下,您可能想使用 Sinatra 或其他东西来代替)。在这种情况下,Rails 可能应该不渲染任何内容,并让 JS 完成所有渲染。

请注意这里的基本原则。服务器应该负责所有渲染,或者客户端应该负责。分裂它会让生活变得更加困难。

Where should you put your Backbone templates? I'd say nowhere. I believe that in most Rails applications, the server should be responsible for all rendering of HTML, while the client-side JavaScript should just be responsible for inserting that rendered HTML into the DOM. Among other things, this makes I18n easier.

The exception would be if Rails is simply being used as a lightweight backend for an application that runs mostly on the client side (though in that case, you might want to use Sinatra or something instead). In this case, Rails should probably render nothing, and have the JS do all the rendering.

Notice the underlying principle here. Either the server should be responsible for all rendering, or the client should. Splitting it will make life harder.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文