【问题标题】:Using Mustache templates and Slim in rails在 Rails 中使用 Mustache 模板和 Slim
【发布时间】:2011-10-20 21:00:54
【问题描述】:
我刚刚阅读了this article,显然Mustache 是模板未来的一部分。我喜欢 HTML 的简洁外观,只是混合了一些小胡子变量。
如何在我的 Rails 3.1 应用程序中结合使用 Mustache 和 Slim 模板语言?是否可以将它与Draper gem 一起使用,或者我是否可以混合使用许多不同的技术?
我尝试使用 mustache_rails3 gem,但它似乎只是为我的应用程序增加了另一层复杂性,而使用 Draper gem 似乎是一种更清洁的解决方案。不过,我对他们中的任何一个都没有太多经验,所以如果有这方面知识的人来这里会很酷。
【问题讨论】:
标签:
ruby-on-rails-3.1
decorator
mustache
slim-lang
【解决方案1】:
我一直在做的是在我的大部分视图中使用 slim,并使用 mustache 来呈现数据(我使用的是 javascript 版本)。
在我的苗条模板中:
div class='template-container'
script type='template'
= render 'my_template.mustache'
在我的咖啡脚本中:
myTemplate = $("script[type='template'").html()
$(".template-container").append(Mustache.render(template, data))
mustache 模板就是 html/mustache。
我希望看到一种解决方案,您可以将 slim 和 mustache 结合到一种模板语言中,但目前似乎不存在。此解决方案显然更适合客户端渲染,但使用部分用于服务器端 mustache 模板也可以让您同时使用 slim 和 mustache。