2012-05-02 29 views
4

我想在Mustache视图中使用我的Sinatra助手方法。Sinatra:如何在Mustache视图中使用助手

我这样做:

# in app.rb: 
... 
helpers do 
    def helloworld 
    "helloworld!" 
    end 
end 
get '/' 
    mustache :home 
end 
... 

# in views/home 
class App < Sinatra::Base 
    module Views 
    class Home < Mustache 
     def hello 
     helloworld 
     end 
    end 
    end 
end 

# in home.mustache 
<p>{{hello}}</p> 

它不工作,我有错误消息:

«未定义的局部变量或方法'的HelloWorld”为App ::浏览::首页:0x000000023ebd48»

如何在Mustache视图中使用我的方法助手?

或者,如何直接从home.mustache中使用我的方法助手?像这样:

# in home.mustache 
<p>{{helloworld}}</p> 

非常感谢您的帮助!

回答

1

你应该能够做一个模块的内容:

# app_helpers.rb 
module AppHelpers 
    def helloworld 
    "helloworld!" 
    end 
end 

# app.rb 
helpers AppHelpers 

get '/' 
    mustache :home 
end 

# views/home.rb 
class App < Sinatra::Base 
    module Views 
    class Home < Mustache 
     include AppHelpers 

     def hello 
     helloworld 
    end 
    end 
end