我应该将 Backbone 模板放置在 Rails 框架中的哪个位置?
我是一名试图学习 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过在开始标记中使用两个
%
来转义 erb 符号,并将主干模板放入 Rails 视图中:将在页面中输出以下内容:
将 Backbone 模板直接放入 Rails 视图中是恕我直言,当您尝试学习时,这是最好的选择。您已经在努力应对新概念,无需添加其他障碍。
You can escape the erb symbols by using two
%
in the opening tag, and put your backbone templates in the rails views:will output the following in your page:
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.
从 Rails 3.1 开始,它提供了两个使使用 Backbone 模板变得更容易的功能:资源管道和自动 JST(JavaScript 模板)编译。
在
app/assets
文件夹中创建一个名为templates
的目录。该目录将被资产管道自动选取。接下来,使用扩展名
jst
和您要创建的模板类型ejs
(嵌入式 JavaScript)来命名该目录中的文件。您甚至可以将它们嵌套在目录中。例如:资产管道还允许您通过简单地更改文件扩展名(并包括任何必要的 gems)来使用其他模板语言,例如嵌入式咖啡脚本、mustache、handlebars 等。
现在,要在 Backbone 视图中引用 JST 模板,只需使用文件名的路径:
您可能需要将此行添加到您的
application.js
中:这是一篇很好的文章,它在更多细节: 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 calledtemplates
. 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 creatingejs
(embedded javascript). You can even nest them in directories. For example: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:
You may need to add this line to your
application.js
: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
您应该将 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.