It's very annoying,

Show nothing

- calendar @date do |date|
  = date.day

But output of haml is in my expectation

  <%= calendar @date do |date| %>
    <%= date.day %>
  <% end %>

This is my helper source code.

module CalendarHelper
  require 'pry'

  def widget
    concat link_to("Hello", '')
    concat " "
    concat link_to("Bye", '')
  end
  def calendar(date = Date.today, &block)
    cal_tbl = Calendar.new(self, date, block).table
    # content_tag :div do
    #   cal_tbl
    # end
    # return cal_tbl
  end

  class Calendar < Struct.new(:view, :date, :callback)
    HEADER = %w[Sunday Monday Tuesday Wednesday Thursday Friday Saturday]
    START_DAY = :sunday

    delegate :content_tag, to: :view

    def table
      content_tag :table, class: "calendar" do
        header + week_rows
      end
    end

    def header
      content_tag :tr do
        HEADER.map { |day| content_tag :th, day }.join.html_safe
      end
    end

    def week_rows
      weeks.map do |week|
        content_tag :tr do
          week.map { |day| day_cell(day) }.join.html_safe
        end
      end.join.html_safe
    end

    def day_cell(day)
      content_tag :td, view.capture(day, &callback), class: day_classes(day)
    end

    def day_classes(day)
      classes = []
      classes << "today" if day == Date.today
      classes << "notmonth" if day.month != date.month
      classes.empty? ? nil : classes.join(" ")
    end

    def weeks
      first = date.beginning_of_month.beginning_of_week(START_DAY)
      last = date.end_of_month.end_of_week(START_DAY)
      (first..last).to_a.in_groups_of(7)
    end
  end
end

enter image description here

有帮助吗?

解决方案

As per the haml Git,

A hyphen(-), will run the code but not output the result

An equals sign(=), will output the result of the code

so it should be

= calendar @date do |date|
  = date.day

其他提示

Do this:

- calendar @date do |date|
  = date.day

Well HAML to HTML translation follows this convention. the rails code that is supposed to be running and not shown is started with a - character, it is equivalent to <% rails code%> in HTML
= in HAML is equivalent of HTML <%= rails code %>, the closing <%end%> is auto generated in response to do rails code.

do this

= calendar @date do |date|
  = date.day
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top