【问题标题】:haml use ruby code and plain texthaml 使用 ruby​​ 代码和纯文本
【发布时间】:2014-08-08 15:59:46
【问题描述】:

我是 ruby​​ on rails 的新手(从今天开始),我正在将它与 haml 一起使用。

出于学习的目的,我想自己将 application.html.erb 更改为 application.html.haml

这是我的 application.html.erb:

<!DOCTYPE html>
<html>
<head>
  <title>HB Combat | <%= @title %></title>
  <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
  <%= csrf_meta_tags %>
</head>
<body>

<%= yield %>

<footer>
</footer>
</body>
</html>

所以我想在 haml 中做标题部分,但我该如何以正确的方式做到这一点?

我目前在haml代码中的内容:

!!! 5
%html
    %head
        %title HB Combat |
    %body

我尝试在 | 后面输入=@title 但这不起作用..

所以我希望你们知道我在这里的意思!并且可以帮助我解决标题部分:D

【问题讨论】:

    标签: ruby-on-rails haml plaintext


    【解决方案1】:

    您可以使用多行来组合纯文本和评估代码

    %title
      HB Combat |
      = @title
    

    或者使用一个评估的字符串:

    %title= "HB Combat | #{@title}"
    

    编辑:正如@Unixmonkey 所指出的,字符串插值也适用于评估代码之外:

    %title HB Combat | #{@title}
    

    【讨论】:

    • %title HB Combat | #{@title} 也适用于新版本的 Haml
    • @Unixmonkey 谢谢!完全忘记了这是一个功能。
    猜你喜欢
    • 1970-01-01
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-08
    • 1970-01-01
    • 2022-08-16
    相关资源
    最近更新 更多