简体   繁体   English

在rails中使用小胡子视图模板的最简单方法是什么?

[英]What's the simplest way to use mustache view templates in rails?

I could do: 我可以做:

render :text => Mustache.render(view_template_in_a_string, object_hash)

in my controller, but it seems more conventional to put view_template_in_a_string in it's own viewname.mustache.html file under views/controllername/action.mustache.html like I would with action.html.erb 在我的控制器中,但按照惯例,将view_template_in_a_string放在它自己的viewname.mustache.html文件中views / controllername / action.mustache.html下,就像使用action.html.erb一样

currently I use 目前我使用

gem 'mustache'

for my mustache needs 满足我的胡子需求

How can I use mustache views like I would with erb 如何像使用erb一样使用小胡子视图


I understand that mustache is logic-less, I don't need logic in my views 我知道小胡子没逻辑,我不需要逻辑


my current hack: 我目前的骇客:

# controllers/thing_controller.rb
def some_action
    hash = {:name => 'a name!!'}
    vw = File.read('./app/views/'+params[:controller]+'/'+params[:action]+'.html.mustache') || ""
    render :text => Mustache.render(vw, hash), :layout => true
end

An updated answer since the original solution, mustache-rails, ceases to exist. 由于原始解决方案“胡须栏杆”不复存在,因此有了更新的答案。

The gem is: 宝石是:

https://github.com/agoragames/stache https://github.com/agoragames/stache

And a redundant but simple example to address how to use stache in our rails project, we'll make the beginning of a Noises Demo App. 还有一个多余但简单的示例来解决如何在Rails项目中使用stache,我们将开始创建Noises Demo App。

To start we'll add both the mustache and the stache gem into our Gemfile and run bundle install . 首先,我们将mustachestache宝石都添加到我们的Gemfile中,然后运行bundle install

Then in our config/application.rb we need to tell stache to use mustache (the other available option is handlebars ; also, this and other configuration options can be found on their github page ). 然后,在我们的config/application.rb我们需要告诉stache使用mustache (另一个可用的选项是handlebars ;此外,此配置选项和其他配置选项可以在其github页面上找到)。

Stache.configure do |config|
  config.use :mustache
end

Here's the sample directory structure with a few example files for our app: 这是示例目录结构,其中包含我们应用程序的一些示例文件:

app/
  controllers/
    noises_controller.rb
    models/
      noise.rb
  templates/ 
    noises/
      animal.mustache
  views/ 
    noises/
      animal.rb 

In controllers/noises_controller.rb controllers/noises_controller.rb

class NoisesController < ApplicationController
  def animal
  end
end

In models/noise.rb models/noise.rb

class Noise < ActiveRecord::Base 
  def self.dog 
    "ar roof"
  end

  def self.sheep 
    "baa"
  end 

  def self.bullfrog 
    "borborygmus"
  end 
end

In templates/noises/animal.mustache templates/noises/animal.mustache

<p>This is what a dog sounds like: {{ dog }}</p>
<p>This is what a sheep sounds like: {{ sheep }}</p>
<p>And bullfrogs can sound like: {{ bullfrog }}</p>

In views/noises/animal.rb views/noises/animal.rb

module Noises 
  class Animal < Stache::Mustache::View 
    def dog 
      Noise.dog
    end 

    def sheep 
      Noise.sheep 
    end 

    def bullfrog 
      Noise.bullfrog 
    end 
  end
end

Hopefully this clarifies an example of how someone can use stache and mustache in a rails application to serve the correct view templates. 希望这可以阐明一个示例,说明有人如何在Rails应用程序中使用stachemustache提供正确的视图模板。

Just use this gem: 只需使用以下宝石:

https://github.com/josh/mustache-rails https://github.com/josh/mustache-rails

That way you can easily configure your rails application to serve the correct view templates, so that you no longer need your hack. 这样,您可以轻松地配置Rails应用程序以提供正确的视图模板,从而不再需要黑客。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Rails JS 模板的正确方法是什么? - What is the correct way to use Rails JS templates? 在Rails中使用Mustache渲染邮件程序模板 - Rendering mailer templates with Mustache in Rails 在Rails模式中启用UUID的最简单的最小特权方法是什么? - What's the simplest least-privilege way to enable UUIDs in a Rails schema? 在Rails 4中使用小胡子模板作为局部视图? - Using mustache templates as partial views in Rails 4? 使一组方法可用于多个Rails应用程序的最简单方法是什么 - What is the simplest way to make a group of methods available to several Rails applications 在Rails中将:pluck或:collect结果转换为字符串数组的最简单方法是什么? - What is simplest way to convert :pluck or :collect results to array of strings in Rails? 在Rails观点中处理英语多元性的最佳方法是什么? - What's the best way to deal with English pluralisation in a rails view? Ruby On Rails:命名嵌套控制器和视图的正确方法是什么 - Ruby On Rails : What's the correct way to naming a nested controller and view 在AngularJ中使用部分视图的最佳方法是什么(在Rails中)? - What is the best way to use partial view for AngularJs (in Rails)? 使用Haml + Mustache在JS和Rails之间共享模板 - Sharing templates beetween JS and Rails using Haml + Mustache
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM