我在Midman中从默认的layout.erb
文件切换到等效的layout.haml
文件时遇到了问题。我想在我的布局中使用HAML,因为我试图熟悉它,所以我决定用HAML版本替换默认布局。不幸的是,当我用layout.erb
替换layout.haml
时,我收到此错误:
NoMethodError at /
undefined method `force_encoding' for nil:NilClass
Ruby /Library/Ruby/Gems/2.0.0/gems/tilt-1.4.1/lib/tilt/template.rb: in ensure in binary, line 289
Web GET localhost/
Traceback (innermost first)
/Library/Ruby/Gems/2.0.0/gems/tilt-1.4.1/lib/tilt/template.rb: in ensure in binary
string.force_encoding(original_encoding)...
/Library/Ruby/Gems/2.0.0/gems/tilt-1.4.1/lib/tilt/template.rb: in binary
string.force_encoding(original_encoding)...
/Library/Ruby/Gems/2.0.0/gems/tilt-1.4.1/lib/tilt/template.rb: in extract_magic_comment
binary script do...
/Library/Ruby/Gems/2.0.0/gems/tilt-1.4.1/lib/tilt/template.rb: in extract_encoding
extract_magic_comment(script) || script.encoding...
/Library/Ruby/Gems/2.0.0/gems/tilt-1.4.1/lib/tilt/template.rb: in precompiled
template_encoding = extract_encoding(template)...
/Library/Ruby/Gems/2.0.0/gems/tilt-1.4.1/lib/tilt/erb.rb: in precompiled
source, offset = super...
/Library/Ruby/Gems/2.0.0/gems/tilt-1.4.1/lib/tilt/erb.rb: in precompiled
source, offset = super...
/Library/Ruby/Gems/2.0.0/gems/tilt-1.4.1/lib/tilt/template.rb: in compile_template_method
source, offset = precompiled(locals)...
/Library/Ruby/Gems/2.0.0/gems/tilt-1.4.1/lib/tilt/template.rb: in compiled_method
compile_template_method(locals_keys)...
/Library/Ruby/Gems/2.0.0/gems/tilt-1.4.1/lib/tilt/template.rb: in evaluate
method = compiled_method(locals.keys)...
/Library/Ruby/Gems/2.0.0/gems/tilt-1.4.1/lib/tilt/template.rb: in render
evaluate scope, locals || {}, &block...
/Library/Ruby/Gems/2.0.0/gems/middleman-core-3.3.3/lib/middleman-core/core_extensions/rendering.rb: in render_individual_file
content = template.render(context, locs, &block)...
/Library/Ruby/Gems/2.0.0/gems/middleman-core-3.3.3/lib/middleman-core/core_extensions/rendering.rb: in render_template
content = render_individual_file(layout_path, locs, opts, context) { content }...
/Library/Ruby/Gems/2.0.0/gems/middleman-core-3.3.3/lib/middleman-core/sitemap/resource.rb: in block in render
app.render_template(source_file, locs, opts, blocks)...
/Library/Ruby/Gems/2.0.0/gems/activesupport-4.1.4/lib/active_support/notifications.rb: in instrument
yield payload if block_given?...
/Library/Ruby/Gems/2.0.0/gems/middleman-core-3.3.3/lib/middleman-core/util.rb: in instrument
::ActiveSupport::Notifications.instrument(suffixed_name, payload, &block)...
/Library/Ruby/Gems/2.0.0/gems/middleman-core-3.3.3/lib/middleman-core/application.rb: in instrument
delegate :instrument, to: ::Middleman::Util...
/Library/Ruby/Gems/2.0.0/gems/middleman-core-3.3.3/lib/middleman-core/sitemap/resource.rb: in instrument
delegate :logger, :instrument, to: :app...
/Library/Ruby/Gems/2.0.0/gems/middleman-core-3.3.3/lib/middleman-core/sitemap/resource.rb: in render
instrument 'render.resource', path: relative_source, destination_path: destination_path do...
/Library/Ruby/Gems/2.0.0/gems/middleman-core-3.3.3/lib/middleman-core/core_extensions/request.rb: in process_request
output = resource.render do...
/Library/Ruby/Gems/2.0.0/gems/middleman-core-3.3.3/lib/middleman-core/core_extensions/request.rb: in block in call!
process_request(env, req, res)...
/Library/Ruby/Gems/2.0.0/gems/middleman-core-3.3.3/lib/middleman-core/core_extensions/request.rb: in catch
catch(:halt) do...
/Library/Ruby/Gems/2.0.0/gems/middleman-core-3.3.3/lib/middleman-core/core_extensions/request.rb: in call!
catch(:halt) do...
/Library/Ruby/Gems/2.0.0/gems/middleman-core-3.3.3/lib/middleman-core/core_extensions/request.rb: in call
dup.call!(env)...
/Library/Ruby/Gems/2.0.0/gems/rack-1.5.2/lib/rack/builder.rb: in call
to_app.call(env)...
/Library/Ruby/Gems/2.0.0/gems/rack-1.5.2/lib/rack/urlmap.rb: in block in call
return app.call(env)...
/Library/Ruby/Gems/2.0.0/gems/rack-1.5.2/lib/rack/urlmap.rb: in each
@mapping.each do |host, location, match, app|...
/Library/Ruby/Gems/2.0.0/gems/rack-1.5.2/lib/rack/urlmap.rb: in call
@mapping.each do |host, location, match, app|...
/Library/Ruby/Gems/2.0.0/gems/rack-1.5.2/lib/rack/showexceptions.rb: in call
@app.call(env)...
/Library/Ruby/Gems/2.0.0/gems/rack-1.5.2/lib/rack/head.rb: in call
status, headers, body = @app.call(env)...
/Library/Ruby/Gems/2.0.0/gems/rack-1.5.2/lib/rack/lint.rb: in _call
status, headers, @body = @app.call(env)...
/Library/Ruby/Gems/2.0.0/gems/rack-1.5.2/lib/rack/lint.rb: in call
dup._call(env)...
/Library/Ruby/Gems/2.0.0/gems/rack-1.5.2/lib/rack/builder.rb: in call
to_app.call(env)...
/Library/Ruby/Gems/2.0.0/gems/rack-1.5.2/lib/rack/handler/webrick.rb: in service
status, headers, body = @app.call(env)...
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/webrick/httpserver.rb: in service
si.service(req, res)...
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/webrick/httpserver.rb: in run
server.service(req, res)...
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/webrick/server.rb: in block in start_thread
block ? block.call(sock) : run(sock)
layout.erb
看起来像这样:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<!-- Always force latest IE rendering engine or request Chrome Frame -->
<meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
<!-- Use title if it's in the page YAML frontmatter -->
<title><%= current_page.data.title || "The Middleman" %></title>
<%= stylesheet_link_tag "normalize" %>
<!-- <%= stylesheet_link_tag "normalize", "all" %> -->
<%= javascript_include_tag "all" %>
</head>
<body class="<%= page_classes %>">
<%= partial "header" %>
<%= yield %>
<%= partial "footer" %>
</body>
</html>
layout.haml
与layout.erb
的内容完全相同,但这似乎并不重要,因为它无论如何都会中断:
!!!
%html
%head
%title
= current_page.data.title || "The Middleman"
= stylesheet_link_tag "normalize", "all"
= javascript_include_tag "all"
%body
= partial "header"
= yield
= partial "footer"
起初我认为错误只是因为HAML本身被破坏了,但即使是一个非常简单的HAML文件,它只定义了doctype,html,head,title和body标签。然后我怀疑只有ERb文件被用作布局文件 - 我通过将layout.haml
重命名为layout.erb
来确认,这不会导致错误,而只是显示原始HAML代码。在导航到博客文章时,即使保留layout.erb
并制作子布局article_layout.haml
也会导致类似错误,同时使article_layout.erb
正常工作。
所以,我怀疑Middleman刚刚接受了除ERb之外的任何内容以获取布局文件。但是,我无法在文档中找到关于此内容的单一内容,而且我甚至看到过所有layout.haml
个文件都能正常运行的示例。我的与众不同吗?是否需要使用middleman init
设置标志才能将HAML文件用于布局?发生了什么事?
答案 0 :(得分:3)
正如@ r.pazyaquian在子注释中提到的,如果更改布局文件类型,请确保重新启动服务器。