[Rails] Alternative to <%=

B

Brian Takita

Hello,

Is there another way to render html to the final page without using <%=
?

I tried using print, but the output was the Webrick console.

Thank you,
Brian Takita
 
J

Jamis Buck

Hello,

Is there another way to render html to the final page without using
<%=
?

I tried using print, but the output was the Webrick console.

You can try:

<% _erbout.concat "hello" %>

But note that this is kind of fragile, because ActionView may
(theoretically) change the name of the concatenation variable
("_erbout").

- Jamis
 
C

Charles Steinman

Brian said:
Is there another way to render html to the final page without using <%=
?

I tried using print, but the output was the Webrick console.

In ERb? No, that's how you do it. What's wrong with the syntax as it
exists?
 
G

Gennady Bystritksy

Brian said:
Hello,

Is there another way to render html to the final page without using <%=
?

I tried using print, but the output was the Webrick console.

Thank you,
Brian Takita

From a controller method responsible for generating the page, you can
use method render:)text => "content"), where "content" is an html
content to show in the browser.

If you are looking for alternatives to ERB, you can try Amrita2
(http://amrita2.rubyforge.org/). Or wait for my soon-to-be-released
XHTML template expansion library called "Facial" (heavily influenced by
Amrita2, see http://facial.rubyforge.org/wiki/wiki.pl for more
information) ;-).

Gennady.
 
B

Brian Takita

Not that this is the best way to do things, but I was thinking of:

<p<%= ' style="text-align: center"' if @has_style%><%= '...' if
@show_another_attribute%>>
Paragraph
</p>

vs:

<%
out = '<p'
out << ' style="text-align: center"' if @has_style
out << '...' if @show_another_attribute
out << '>'
out << Paragraph
out << '</p>

print out
%>

But now that I think about it, something like this would work out
better:

<%=
begin
out = '<p'
out << ' style="text-align: center"' if @has_style
out << '...' if @show_another_attribute
out << '>'
out << Paragraph
out << '</p>
end
%>
 
J

Jay Levitt

Or wait for my soon-to-be-released
XHTML template expansion library called "Facial"

You many want to rethink the name if you expect this to become
popular... "facial" has a noun has some particularly bad connotations.
 
J

Jamis Buck

But now that I think about it, something like this would work out
better:

<%=
begin
out = '<p'
out << ' style="text-align: center"' if @has_style
out << '...' if @show_another_attribute
out << '>'
out << Paragraph
out << '</p>
end
%>

This looks like a prime candidate for refactoring into a helper,
actually:

<p<%= my_custom_p_attrs %>>Paragraph</p>

With the corresponding helper definition:

def my_custom_p_attrs
attrs = ""
attrs << %{ style="text-align: center"} if @has_style
attrs << %{ ...} if @show_another_attribute
attrs
end

- Jamis
 
C

Charles Steinman

Brian said:
But now that I think about it, something like this would work out
better:

<%=
begin
out = '<p'
out << ' style="text-align: center"' if @has_style
out << '...' if @show_another_attribute
out << '>'
out << Paragraph
out << '</p>
end
%>

I would wrap something like that in a helper method. Although you may
want to look into just using CSS classes as well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,946
Members
47,498
Latest member
yelene6679

Latest Threads

Top