How to avoid bunch of <% %>?

Hello, I would like to know is there is a way to avoid poluting the
views with tons of <% %> ?

Like this
<%= start_form_tag() %>
<%= text_field_tag(‘category[title]’, category.title, {:size => 20,
:maxlength => 128}) %>
<% if not category.parent_id.nil? %>
<%= select(“category”, “parent_id”,
Category.find(:all, :conditions => [ “id <> ?”,
category.id] ).collect {|c| [ c.title, c.id ] },
:selected => (category.parent_id.nil? ? “” :
category.parent.id)) %>
<% end %>
<%= submit_tag “Enregistrer” %>
<%= link_to “Annuler”, :action => “list” %>
<%= end_form_tag() %>

Obviously this is not very clean. I find it hard to read and having to
type so many times the same symbols is tedious

Thanks

You can give Markaby a try:

http://code.whytheluckystiff.net/markaby

If you’re using the rhtml templates, there are always going to be a
fair number of <% %> symbols to type. I use TextMate, which has
helpful key bindings for that. But some things that might help clean
things up a bit:

  • Try using form_for: Peak Obsession
    Helpers/FormHelper.html#M000384
  • Try putting that select tag into a helper.

For example: (though my syntax might be incorrect as it’s late)

<% form_for :category, category do |f| %>
<%= f.text_field :title, :size => 20, :maxlength => 128 %>
<%= f.select :parent_id, category_selections(category) unless
category.parent_id.nil? %>

 <%= submit_tag "Enregistrer">
 <%= link_to "Annuler", :action => "list" %>

<% end %>

And with your helpers:

def category_selections(category)
Category.find(:all, :conditions => [“id <> ?”,
category.id]).collect {|c| [c.title, c.id]}
end

However, that’s only two fewer <% %> tags, and only because of the
way I used “unless”.

– Michael D.
http://www.mdaines.com

Nuno wrote:

Hello, I would like to know is there is a way to avoid poluting the
views with tons of <% %> ?

Like this
<%= start_form_tag() %>
<%= text_field_tag(‘category[title]’, category.title, {:size => 20,
:maxlength => 128}) %>
<% if not category.parent_id.nil? %>
<%= select(“category”, “parent_id”,
Category.find(:all, :conditions => [ “id <> ?”,
category.id] ).collect {|c| [ c.title, c.id ] },
:selected => (category.parent_id.nil? ? “” :
category.parent.id)) %>
<% end %>
<%= submit_tag “Enregistrer” %>
<%= link_to “Annuler”, :action => “list” %>
<%= end_form_tag() %>

Obviously this is not very clean. I find it hard to read and having to
type so many times the same symbols is tedious

For the sake of clarity, I assume you’re not trying to drop all the <%
markup, but are in fact looking for a way to put the whole thing inside
a single <%.

In effect, can we convert <%= %> into something like print(…) so we
can embed <%= %> inside <% %> ?

For something as complicated as above, you may find helpers useful, but
even in a simple case:

<% for blah in blahs %>
<%= blah.foo %>
<% end %>

I’d prefer to do:

<%
for blah in blahs
print blah.foo
end
%>

Which would, as a side effect, facilate easy moving into a helper.

I’d like to know how to do this as well. Anyone with any ideas ?

A.

snip

<%
for blah in blahs
print blah.foo
end
%>

Which would, as a side effect, facilate easy moving into a helper.

I’d like to know how to do this as well. Anyone with any ideas ?

A.

Me too!!!

WOuld really clean up my RHTML, especially when there isn’t much other
than ruby helper output and ruby iterators/conditions.

P

More Like…

<%= start_form_tag() = text_field_tag('category[title]', category.title, {:size => 20, :maxlength => 128}) if not category.parent_id.nil? = select("category", "parent_id", Category.find(:all, :conditions => [ "id <> ?", category.id] ).collect {|c| [ c.title, c.id ] }, :selected => category.parent_id.nil? ? "" : category.parent.id)) end %>

Some valid HTML here....

<% = submit_tag "Enregistrer" = link_to "Annuler", :action => "list" = end_form_tag() %>

If erb could interpret a lonely = sign at the start of a line in the
same way as
<%=bleh%>

P

Paulie wrote:

%>

Some valid HTML here…

<%
= submit_tag “Enregistrer”
= link_to “Annuler”, :action => “list”
= end_form_tag()
%>

If erb could interpret a lonely = sign at the start of a line in the
same way as
<%=bleh%>

How about a ‘+’ at the end of a line?

Some valid HTML here....

<%= submit_tag "Enregistrer" + link_to("Annuler", :action => "list") + end_form_tag() %>

Paulie wrote:

<%
for blah in blahs
print blah.foo
end
%>

Me too!!!

WOuld really clean up my RHTML, especially when there isn’t much other
than ruby helper output and ruby iterators/conditions.

I’d do that as:

<%=
blahs.collect{ |blah|
render_blah blah
}.join
%>

Hmmmmm, never thought of that :slight_smile:

P

Actually, I think what your proposing is really interesting.

For instance, this is how we’d loop a collection (lets say partials w/
collections are overkill here)

We’d normally write

<% for user in @users -%>
<%= user.name %>

<% end -%>

We could write.

<% for user in @users
= user.name + “

end %>

Now, the problem with having erb syntax that allows that is we can get
all
non-mvc like temptations. Aka, mixing in our controller code with our
view.

But, then again, I think the second option is much more pretty. I mean,
the
lack of this is basically what requires -%>… so that you can keep from
messing up your response html.

Thoughts?

-hampton.

But how to deal with non string producer tokens ? Seems difficult with
concatenation

I believe this can be achieved with concat(), though I’ve never used it
myself.

Hampton wrote:

We could write.

<% for user in @users
= user.name http://user.name + “

end %>
Or, going back to where I first came in…
<%= @users.map{|user|
user.name + “

} %>

On 4/6/06, Hampton [email protected] wrote:

Now, the problem with having erb syntax that allows that is we can get all
non-mvc like temptations. Aka, mixing in our controller code with our view.

But, then again, I think the second option is much more pretty. I mean, the
lack of this is basically what requires -%>… so that you can keep from
messing up your response html.

Maybe it’s worth exploring the possibility that the Java world got
something right. Custom tags, especially like those from Struts, go a
long way toward cleaning up the view.

<logic:iterate name=“someform” property=“someprop” id=“element”>

</logic:iterate>

or

<% for element in @someprop -%>

<% end -%>

Is one cleaner than the other? For simple things, probably not. But
for more complex views, I also have this nagging feeling of annoyance
at all the <% %> tags.

– James

Mark Van H. wrote:

<%
_erbout << “some cocntent”
%>
There’s something about using _erbout that strikes me as really, really
icky. I just can’t put my finger on it…

<%
_erbout << “some cocntent”
%>

mark

That’ll be the “really I’m private” underscore. How about using
helpers instead? Otherwise, give the Amrita plugin a try, or possibly
define a function like:

def print(stuff)
_erbout << stuff
end

Douglas

2006/4/6, Alex Y. [email protected]:

since there are 17 replies all containins <% %>s, i will say it again,
check out Markaby !!

its pretty damn near perfection in terms of minimizing the amount of
typing, let alone typing of < % >s, if you are able to mentally
visualize your pages layout from a handcoding perspective.

it has a few bugs, like i cant get instance variables created inside a
markaby template, to propagate to partials, without explicitly passing
them in via the :locals flag. and if you want to have a designer work on
your templates, dreamweaver etc will surely choke on them… but come on,
teach your designer to code :wink:

Ah, how about we extend the actionview to handle this!

<% for user in @users
<< user.name + “”
end %>

And, the way this would be handled would be that there is…

def <<(input)
_erabout << input + “\n”
end

Another example…

<% for @row in @rows
<< start_form_tag
<< text_field(:row, :name)
<< hidden_field(:row, :id)
<< end_form_tag
<< “\n”
end %>

or

<% for @row in @rows
<< start_form_tag << text_field(:row, :name) << hidden_field(:row,
:id)
<< end_form_tag << “\n”
end %>

instead of

<% for @row in @rows -%>
<%= start_form_tag %>
<%= text_field :row, :name %>
<%= hidden_field(:row, :id) %>
<%= end_form_tag %>
<% end -%>

Thoughts?

-hampton.

I don’t know, there is something very ugly to me about…

<% for @row in @rows
print start_form_tag
print text_field(:row, :name)
print hidden_field(:row, :id)
print end_form_tag
print “\n”
end %>

I much prefer something more symbolic. Maybe I’m just weird.

-hampton.