Rails sprawdzają, czy w treści jest zdefiniowany yield:area dla

Chcę zrobić renderowanie warunkowe na poziomie układu na podstawie rzeczywistego zdefiniowanego szablonu content_for(:an__area), jakiś pomysł jak to zrobić?

Author: wintermeyer, 2008-10-11

6 answers

@content_for_whatever jest przestarzały. Użyj content_for? zamiast tego, w ten sposób:

<% if content_for?(:whatever) %>
  <div><%= yield(:whatever) %></div>
<% end %>
 220
Author: gudleik,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-10 15:15:26

Nie jest konieczne tworzenie metody pomocniczej:

<% if @content_for_sidebar %>
  <div id="sidebar">
    <%= yield :sidebar %>
  </div>
<% end %>

NO to oczywiście Twoim zdaniem:

<% content_for :sidebar do %>
  ...
<% end %>

Używam tego cały czas, aby warunkowo przejść między jedną kolumną a dwiema kolumnami

 10
Author: efalcao,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-03-11 20:36:57
<%if content_for?(:content)%>
  <%= yield(:content) %>
<%end%>
 2
Author: gregwinn,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-02-18 16:49:28

Można utworzyć helper:

def content_defined?(var)
  content_var_name="@content_for_#{var}"    
  !instance_variable_get(content_var_name).nil?
end

I użyj tego w swoim układzie:

<% if content_defined?(:an__area) %>
  <h1>An area is defined: <%= yield :an__area %></h1>
<% end %>
 2
Author: Nick B,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-10-07 00:50:30

Ok mam zamiar bezwstydnie zrobić sobie odpowiedź, ponieważ nikt nie odpowiedział i już znalazłem odpowiedź :) Zdefiniuj ją jako metodę pomocniczą w application_helper.rb lub gdziekolwiek uznasz to za wygodne.

  def content_defined?(symbol)
    content_var_name="@content_for_" + 
      if symbol.kind_of? Symbol 
        symbol.to_s
      elsif symbol.kind_of? String
        symbol
      else
        raise "Parameter symbol must be string or symbol"
      end

    !instance_variable_get(content_var_name).nil?

  end
 1
Author: William Yeung,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-10-11 16:01:17

Nie jestem pewien, jaki wpływ na wydajność ma dwukrotne wywołanie yield, ale będzie to miało miejsce niezależnie od wewnętrznej implementacji yield (@content_for_xyz jest przestarzały) i bez dodatkowego kodu lub metod pomocniczych:

<% if yield :sidebar %>
  <div id="sidebar">
    <%= yield :sidebar %>
  </div>
<% end %>
 1
Author: Enrico,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-11-16 12:01:49