在编写ruby代码时haml与=和 - 有什么区别?

时间:2021-08-23 13:23:13

So I am a newbie at HAML.

所以我是HAML的新手。

While going through the HAML tutorial the way to represent ruby code was mentioned as =

在浏览HAML教程时,表示ruby代码的方式被称为=

eg: %strong= item.title

例如:%strong = item.title

but when I ran this code:

但是当我运行这段代码时:

= @list.documents.each do |doc|
  %tbody
    %tr
      %td= doc.display_name

along with all the list data which was displayed there was also alot of junk data displayed which was related to the actual list data which was displayed. This is what I got:

除了显示的所有列表数据之外,还显示了很多与显示的实际列表数据相关的垃圾数据。这就是我得到的:

val1 val2 val3 [#Document......@id : val1, @id:val2.....]

When i try the same code while replacing the = with a - the unwanted data is not received.

当我用 - 替换=时尝试相同的代码 - 不接收不需要的数据。

- @list.documents.each do |doc|
      %tbody
        %tr
          %td= doc.display_name

output:

val1 val2 val3

can someone explain the difference between - and = while writing ruby code in haml?

有人可以在haml中编写ruby代码时解释 - 和=之间的区别吗?

2 个解决方案

#1


6  

They are both indicators that what follows needs to be processed as Ruby code. But - doesn't output the result to view whereas = does.

它们都是需要作为Ruby代码处理的指标。但是 - 不会将结果输出到view而是=。

For example, consider the following helper:

例如,请考虑以下帮助程序:

def hlp
  [1,2].each(&:succ) # using example relevant to your code sample
end

each will return the enumerator that it is called upon. So the return value of that helper method will be [1,2].

每个都将返回调用它的枚举器。因此该辅助方法的返回值为[1,2]。

The following in your HAML view will not display [1,2]:

您的HAML视图中的以下内容将不会显示[1,2]:

- hlp

But the following will display it:

但以下将显示它:

= hlp

Therefore in the sample code you have shared, you will use -, and not =, because you don't want to display all @list.documents once each is done with them.

因此,在您共享的示例代码中,您将使用 - ,而不是=,因为您不希望在完成每个@ list.documents后显示它们。

#2


0  

In haml, = is used to write ruby code and displays the result as output, while - is just used to write ruby code.
You can read more about haml from this http://haml.info/tutorial.html

在haml中,=用于编写ruby代码并将结果显示为输出,而 - 仅用于编写ruby代码。您可以从http://haml.info/tutorial.html上阅读有关haml的更多信息

#1


6  

They are both indicators that what follows needs to be processed as Ruby code. But - doesn't output the result to view whereas = does.

它们都是需要作为Ruby代码处理的指标。但是 - 不会将结果输出到view而是=。

For example, consider the following helper:

例如,请考虑以下帮助程序:

def hlp
  [1,2].each(&:succ) # using example relevant to your code sample
end

each will return the enumerator that it is called upon. So the return value of that helper method will be [1,2].

每个都将返回调用它的枚举器。因此该辅助方法的返回值为[1,2]。

The following in your HAML view will not display [1,2]:

您的HAML视图中的以下内容将不会显示[1,2]:

- hlp

But the following will display it:

但以下将显示它:

= hlp

Therefore in the sample code you have shared, you will use -, and not =, because you don't want to display all @list.documents once each is done with them.

因此,在您共享的示例代码中,您将使用 - ,而不是=,因为您不希望在完成每个@ list.documents后显示它们。

#2


0  

In haml, = is used to write ruby code and displays the result as output, while - is just used to write ruby code.
You can read more about haml from this http://haml.info/tutorial.html

在haml中,=用于编写ruby代码并将结果显示为输出,而 - 仅用于编写ruby代码。您可以从http://haml.info/tutorial.html上阅读有关haml的更多信息