-
Notifications
You must be signed in to change notification settings - Fork 30
Rendering Strings
catmando edited this page Oct 27, 2015
·
2 revisions
Some special rules apply when strings are rendered. By default when any render method or tag block returns a string the string is wrapped in a span
tag.
For example
def render
"my fat string"
end
Will generate <span>My fat string</span>
and
def render
div { "in a div" }
end
will generate <div><span>in a div</span></div>
However this only works for the string actually returned.
Say you have
def render
div do
"text 1"
"text 2"
"text 3"
end
end
You will only get <div>text 3</div>
Instead you probably want to do this:
def render
div do
span {"text 1"}
span {"text 2"}
span {"text 3"} # this span is not necessary but it makes things neater
end
end
To make things a bit shorter you can send attach the following tags directly to a string span
, para
(the p tag), td
, th
and br
. For example
table do
tr do
"column 1".td
"column 2".td
"column 3".td
end
end
Note that you can pass parameters to the methods and attach classes as normal. For example
"foo".span(style: {font_size: "20px"}).my_class
renders
<span class="my-class" style="font-size: '20px'">foo</span>