使用中间人,如何将一个 HAML 文件包含在另一个 HAML 文件中?

发布于 2025-01-08 05:25:29 字数 707 浏览 0 评论 0原文

我正在使用中间人进行一些快速原型设计,但我一生都无法弄清楚如何将一个 HAML 文件包含到另一个 HAML 文件中。

我可以在布局文件中包含内容,但无法让一个非布局文件包含另一个非布局文件。我想在某些页面上重用一些 HTML 块,我想我可以做到这一点。我已经尝试过:

- render: partial=>"shared/nav.haml"
=shared/nav.html
="shared/nav.html

但这些都不起作用。

我是否缺少配置选项或插件?这是一个全新的中间人安装。


答案

部分可能需要以下划线开头的文件名。我的部分内容放置在名为共享的文件夹中。文件的全名是 _nav.html.haml

这对我有用。

!= haml :"shared/_nav"

上下文示例:

#email.main.subscriber.resize
  #bg-wrap
    %div
      %img{:src=>"images/backgrounds/image.png",:alt=>""}
  %section#zone10
    != haml :"shared/_nav"

您还可以使用下面批准的答案中指定的格式。

I'm using middleman to do some rapid prototyping and can't for the life of me figure out how to include one HAML file into another HAML file.

I can include stuff in a layout file, but can't get one non-layout file to include another non-layout file. There are blocks of HTML that I want to reuse on some pages and I think I could do this. I've tried:

- render: partial=>"shared/nav.haml"
=shared/nav.html
="shared/nav.html

and none of these work.

Am I missing a config option or plugin? This is a fresh middleman install.


ANSWER

Partials may need file names that start with an underscore. My partial is placed in a folder called shared. The full name of the file is _nav.html.haml

This worked for me.

!= haml :"shared/_nav"

Example in context:

#email.main.subscriber.resize
  #bg-wrap
    %div
      %img{:src=>"images/backgrounds/image.png",:alt=>""}
  %section#zone10
    != haml :"shared/_nav"

You may also use the format specified in the approved answer below.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鼻尖触碰 2025-01-15 05:25:29

我一直在 MiddleMan 中使用 HAML,并且非常高兴。以下是对我有用的内容:

我有一个文件: source/_donate_buttons.h

 #DonationButtons
   %p= t('searching.donate_cover_costs')
   %br
   = partial(:paypal_donate_button, :locals => {:amount => 1, 
     :amount_text => t('searching.donate_1')})

这使用显示的部分语句来包含名为 source/_paypal_donate_button.html.haml 的文件。

我将 _donate_buttons.html.haml 文件本身包含在几个地方:

= partial "donate_buttons"

虽然我认为这也可能是:

= partial :donate_buttons

即我认为 partial 是你的魔力寻找。

而且,为了完整起见,这里有一个稍微精简的 _paypal_donate_button.haml,它显示了参数化如何在那里工作:

-btnclass = (locals.key?(:highlight) && locals[:highlight] ? "HighlightedDonationButton" : "DonationButton")
-btnstyle = locals.key?(:button_style) && locals[:button_style]
.DonationButtonContainer
  %form{:action => "https://www.paypal.com/cgi-bin/webscr", :method => "post"}
    %input{:name => "business", :type => "hidden", :value => "[email protected]"}
    %input{:name => "cmd", :type => "hidden", :value => "_donations"}
    %input{:name => "amount", :type => "hidden", :value => "#{amount}.00"}
    %input{:name => "currency_code", :type => "hidden", :value => "USD"}
    %input{:class => btnclass, :alt => t('paypal.alt_text'),
      :style => "cursor: pointer; font-size: 18px; #{btnstyle}", :type => "submit", :value => amount_text}

Fwiw,我认为该文件不需要是 _filename.html.haml 也可以是 _filename.haml。另外,我正在本地化这些,因此忽略 t('tagname') 并将字符串放在那里。 (我不想在复制粘贴示例时引入错误,因此我将它们留在那里。)

希望这会有所帮助!

I've been using HAML with MiddleMan and couldn't be happier. Here is what is working for me:

I have a file: source/_donate_buttons.h

 #DonationButtons
   %p= t('searching.donate_cover_costs')
   %br
   = partial(:paypal_donate_button, :locals => {:amount => 1, 
     :amount_text => t('searching.donate_1')})

This uses the partial statement shown to include a file called source/_paypal_donate_button.html.haml.

And I include the _donate_buttons.html.haml file itself in a couple of places with:

= partial "donate_buttons"

though I think this could also be:

= partial :donate_buttons

I.e. I think partial is the magic you're looking for.

And, just for completeness, here is a slightly stripped down _paypal_donate_button.haml which shows how the paramaterization works there:

-btnclass = (locals.key?(:highlight) && locals[:highlight] ? "HighlightedDonationButton" : "DonationButton")
-btnstyle = locals.key?(:button_style) && locals[:button_style]
.DonationButtonContainer
  %form{:action => "https://www.paypal.com/cgi-bin/webscr", :method => "post"}
    %input{:name => "business", :type => "hidden", :value => "[email protected]"}
    %input{:name => "cmd", :type => "hidden", :value => "_donations"}
    %input{:name => "amount", :type => "hidden", :value => "#{amount}.00"}
    %input{:name => "currency_code", :type => "hidden", :value => "USD"}
    %input{:class => btnclass, :alt => t('paypal.alt_text'),
      :style => "cursor: pointer; font-size: 18px; #{btnstyle}", :type => "submit", :value => amount_text}

Fwiw, I don't think the file needs to be _filename.html.haml and can instead be _filename.haml. Also, I'm localizing these, so ignore the t('tagname') and just put strings there. (I didn't want to introduce an error copy-pasting the examples so I left them in there.)

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文