Conditional Tag Wrapping in Rails/Erb

Conditional tag wrapping in Rails / ERB

If you really want the DIV to be conditional, you could do something like this:

put this in application_helper.rb

  def conditional_div(options={}, &block)
if options.delete(:show_div)
concat content_tag(:div, capture(&block), options)
else
concat capture(&block)
end
end

which then you can use like this in your view:

<% @items.each do |item| %>
<% conditional_div(:show_div => item.isolated?, :class => 'isolated') do %>
<%= item.name.pluralize %>
<% end %>
<% end %>

How can I conditionally wrap some HAML content in a tag?

You could use a partial.

foo.html.haml

- if i_should_link
%a{:href => url}
= render 'bar'
- else
= render 'bar'

_bar.html.haml

.foo
.block
.of
.code

Edit: Or you could use content for, I guess this is better because it keeps it all in the same file.

- if i_should_link
%a{:href => url}
= yield :foobar
- else
= yield :foobar

- content_for :foobar do
.foo
.block
.of
.code

How to conditionally wrap or indent in Slim

Yes, your approach is probably the best way to achieve what you're looking for.

Prettier ERB conditionally adding element class

I do a lot of CSS modification through ERB. I like to do things like:

<% site.data.navigation.each { |item| %>
<a href="<%= item.link %>"
class="<%= (page.url == item.link) ? 'current', '')%>" >
<%= item.name %>
</a>
<% } %>

While I try to not allow lines to go past the edge of my editor screen, with HTML I don't enforce that as much because I like to see a tag as one line:

 <% site.data.navigation.each do |item| %>
<a href="<%= item.link %>" class="<%= (page.url == item.link) ? 'current', '')%>" >
<%= item.name %>
</a>
<% end %>

For me that shows the outer loop, then the tag, then the contents of the tag, and finally the closing tag. Also I don't use {} for multiline loops.

If else statements in .html.erb in views

Unless you can think of a way to re-write this as a helper method, you're basically stuck with it looking kind of ugly. That's just how ERB is, as it was intended to be a minimal way of injecting Ruby into an otherwise plain-text template, not as something necessarily streamlined or elegant.

The good news is a syntax-highlighting editor will usually make your <% ... %> ERB blocks look visually different from your HTML so that can dramatically improve readability.

It's also why other representations like HAML have been created where that syntax is a lot less cluttered:

- if some_condition_previusly_established_in_a_controller
.one 123
- else
.two something else

Unable to wrap div with anchor tags in rails

You need to use a block for your code:

<%= link_to "/google" do %>
<div class="container">
content
</div>
<% end %>

That code will render the following html

<a href="/google">
<div class="container">
Content
</div>
</a>

ERB end tag for label_tag after a block

Have you tried using content_tag?

# app/helpers/application_helper.rb
class ApplicationHelper
def download_label
content_tag :label, { :for => 'pic1', :class => 'pic_s' } do
content_tag :div do
image_tag('https://upload.wikimedia.org/wikipedia/commons/7/79/Download_link.png')
end
end
end
end

# erb file
<%= download_label %>


Related Topics



Leave a reply



Submit