I have a snippet as following and want to share it in mulitiple templates. Is
there a way to do it?
thanks,
Xianfeng
<%
def output_array(s)
str = ''
if s.is_a?(Array)
if s.length >= 1
str = s.at(0)
str = “”#{str}""
if s.length > 1
for i in 1…(s.length - 1)
w = s.at(i)
str = "#{str}, “#{w}”“
end
end
end
end
”#{str}"
end
%>
I have a snippet as following and want to share it in mulitiple templates.
Is
there a way to do it?
thanks,
Xianfeng
<%
def output_array(s)
str = ''
if s.is_a?(Array)
if s.length >= 1
str = s.at(0)
str = ""#{str}""
if s.length > 1
for i in 1..(s.length - 1)
w = s.at(i)
str = "#{str}, "#{w}""
end
end
end
end
"#{str}"
end
%>
I have a snippet as following and want to share it in mulitiple
templates. Is
there a way to do it?
thanks,
Xianfeng
<%
def output_array(s)
str = ''
if s.is_a?(Array)
if s.length >= 1
str = s.at(0)
str = ""#{str}""
if s.length > 1
for i in 1..(s.length - 1)
w = s.at(i)
str = "#{str}, "#{w}""
end
end
end
end
"#{str}"
end
%>
Personally when I have more complicated logic in a template I'll factor
that out into a library method, which I then call directly in the
template... Only draw back here is that the node data isnt available in
the Library, so I have to pass that into the method...
I have a snippet as following and want to share it in mulitiple
templates. Is
there a way to do it?
thanks,
Xianfeng
<%
def output_array(s)
str = ''
if s.is_a?(Array)
if s.length >= 1
str = s.at(0)
str = ""#{str}""
if s.length > 1
for i in 1..(s.length - 1)
w = s.at(i)
str = "#{str}, "#{w}""
end
end
end
end
"#{str}"
end
%>
I have a snippet as following and want to share it in mulitiple
templates. Is
there a way to do it?
thanks,
Xianfeng
<%
def output_array(s)
str = ''
if s.is_a?(Array)
if s.length >= 1
str = s.at(0)
str = ""#{str}""
if s.length > 1
for i in 1..(s.length - 1)
w = s.at(i)
str = "#{str}, "#{w}""
end
end
end
end
"#{str}"
end
%>
The responses so far are very good answers to the general question. I
think the best solution for your case is John's solution. However, I
thought I might mention that the specific ruby code you posted could be
simplified quite a bit. I would write that as
my_array.join(', ') if my_array.is_a?(Array)
Actually, I just noticed that your method surrounds each element in the
array with escaped double quotes. If you want that, you could do
my_array.map{ |s| ""#{s}"" }.join(', ') if my_array.is_a?(Array)
I have a snippet as following and want to share it in mulitiple
templates. Is
there a way to do it?
thanks,
Xianfeng
<%
def output_array(s)
str = ''
if s.is_a?(Array)
if s.length >= 1
str = s.at(0)
str = ""#{str}""
if s.length > 1
for i in 1..(s.length - 1)
w = s.at(i)
str = "#{str}, "#{w}""
end
end
end
end
"#{str}"
end
%>