Ben*_*Ben 18 ruby haml view sinatra
我有一个简单的sinatra应用程序,使用haml和sass的视图.其中一个视图(位于视图文件夹中)是我的导航菜单的一部分.我试图从index.haml渲染它,但我得到以下错误:错误的参数数量(1为2)
我试图用index.haml中的以下行来渲染它
.navigation
= render :partial => "nav"
Run Code Online (Sandbox Code Playgroud)
jm3*_*jm3 13
或者你可以这样做:
helpers do
def partial(page, options={})
haml page.to_sym, options.merge!(:layout => false)
end
end
Run Code Online (Sandbox Code Playgroud)
并包括你的部分:
= partial( "something-rad" )
Run Code Online (Sandbox Code Playgroud)
这是我如何做到的(比@ kfl62的答案更简单,比@ jm3的答案更丰富功能):
module Partials
def partial( page, variables={} )
haml page.to_sym, {layout:false}, variables
end
end
helpers Partials
Run Code Online (Sandbox Code Playgroud)
在你的Haml文件中使用它,如:
%ul#comments
- @comments.each do |comment|
%li= partial :comment, comment:comment
Run Code Online (Sandbox Code Playgroud)
编辑:!!! 过时!!! 阅读杰森的答案如下!
你在尝试什么工作rails!Sinatra没partial办法.来自github 的partialon 的实现Sinatra看起来像这样(source gist):
module Haml
module Helpers
def partial(template, *args)
template_array = template.to_s.split('/')
template = template_array[0..-2].join('/') + "/_#{template_array[-1]}"
options = args.last.is_a?(Hash) ? args.pop : {}
options.merge!(:layout => false)
if collection = options.delete(:collection) then
collection.inject([]) do |buffer, member|
buffer << haml(:"#{template}", options.merge(:layout =>
false, :locals => {template_array[-1].to_sym => member}))
end.join("\n")
else
haml(:"#{template}", options)
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
包括这种方法,你可以调用partial你的.haml文件,比如
= partial("partial_name")
如果你想在render一个视图中使用其他视图语法
= render(:haml,:'rel_path_to_view',:locals => {:optional => option})
注意之间的语法差异rails和sinatra有关render方法!
| 归档时间: |
|
| 查看次数: |
15832 次 |
| 最近记录: |