Gossamer Forum
Home : Products : Gossamer Links : Development, Plugins and Globals :

Convert linebreaks to list elements

Quote Reply
Convert linebreaks to list elements
Is it possible to have a global to convert the linebreaks in a field so that a block of text is rendered as an ordered or numbered list e.g.:

Code:
<ol>
<li>First line here</li>
<li>Second line here</li>
</ol>

I use the global below to convert linebreaks to <br/> tags, but I haven't had any luck modifying it to do what I want.

Code:
sub {
my $field = $_[0];
$field =~ s/\n/\<br\/\>/gi;
return $field;
}
I want to display a certain field as a method, and a numbered list would be the ideal way to do it.
Quote Reply
Re: [aus_dave] Convert linebreaks to list elements In reply to
*untested*

Code:
sub {
my $val = shift || return;
my $output = '';
foreach (split("\n", $val)) {
$output .= '<li>' . $_ . '</li>' . "\n";
}
return $output;
}

Code:
<ol>
<%global_of_your_choice($field_of_your_choice)%>
</ol>

Hope that helps.
Quote Reply
Re: [Volker] Convert linebreaks to list elements In reply to
Thanks Volker, that works nicely :).

I can see the code is doing what I want it to, although I will have to do some work on the default luna style sheet. The core version has <li> elements in the #content div set as list-style: none; Crazy