ruby - 无法将 HAML 布局与中间人一起使用

标签 ruby layout haml erb middleman

我在从默认 layout.erb 文件切换到 Middleman 中等效的 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.hamllayout.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 文件。只是我的不一样吗?为了使用 HAML 文件进行布局,是否需要使用 middleman init 设置标志?怎么回事?

最佳答案

正如@r.pazyaquian 在子评论中提到的,如果您更改布局文件类型,请确保重新启动服务器。

关于ruby - 无法将 HAML 布局与中间人一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25108471/

相关文章:

ruby - Textmate 不运行最新版本的 ruby

ruby-on-rails - 通过 IMAP 将电子邮件拉入 Rails 应用程序

ruby - Bundler:找到哪个 gem 依赖于此?

html - 在响应式布局中每隔第 n 行偏移一次

ruby-on-rails - Haml 嵌套与 Rails 中渲染的部分

arrays - 将新值附加到 ruby​​ 中的现有 JSON 数组

android - 单击时 android 组件的掩码

css - 强制嵌套 div 的最小高度为 100%?

ruby-on-rails - Bootstrap动态主动导航

ruby - Haml "Illegal Nesting"问题;如何在同一个标​​签中放置多个代码元素?