jbrugger has asked for the wisdom of the Perl Monks concerning the following question:

Hi all

I've got the following issue:

I've got a html table that has a fixed with, given bij a html-template tag from within perl.
In this tablem, i show some user-information, like an email adreds from the user eg:
<table width="<!--TMPL_VAR NAME=table_width -->"<tr><td>youremailadress@domain.com</td></table>
Now if the table is too small, the email has to be broken into pieces. On this moment, i use a function breakLine, and i estimate the width of the mail:
sub breakLine() { my ($string, $length, $marker) = @_; my $tempstring = ""; my $afterstring = ""; if ( length($string) > $length && $length > 0 ) { $length -= length($marker); if ($length <= length($marker)) { $marker = ""; $tempstring = substr( $string, 0, $length ); } else { $afterstring = substr( $string, $length, lengt +h($string)); if (length($afterstring) > $length ) { $afters +tring = &breakLine($afterstring,$length+1, $marker);} $tempstring = substr( $string, 0, $length ) . +$marker . " " . $afterstring; } $string = $tempstring; } $string; } $tmpl->param( useremail => &breakLine($user->{email},$length,"-"), ); print $tmpl->output;

As you understand, no nice and clean solution the text www takes more space than aaa or iii.
Is there a way to solve this problem, so if the table is about 150px, i can let perl adjust the text and create a good breakup? i haven't found this in css (yet) as well, second it should be done in perl, since i use the same sort of templating system to create Latex files as well for PDF creation.

Replies are listed 'Best First'.
Re: breaking words in a table.
by TedPride (Priest) on Feb 28, 2005 at 09:26 UTC
    The best way is probably to insert <wbr> tags at various points and let the browser wrap as necessary. There isn't really a good way to hard wrap text to a desired pixel width.
Re: breaking words in a table.
by TedPride (Priest) on Feb 28, 2005 at 10:35 UTC
    No solution is going to be perfect. Text size can vary significantly across browsers and operating systems, so cropping your text based on number of characters is unreliable at best. At least this way it ends up looking good for the majority of viewers.
      ...and even if you have an audience using a single browser, you can't count on them all running same res, etc, so the observations from TedPride et al (above) --IMO-- are wise.

      However, jdb, you wrote "the text www takes more space than aaa or iii."

      But, pedantically and just FWIW, you could use css to have mail addresses rendered in a "Fixed width font" (think console fonts; tty; typewriter). In that case, the width needed for "www" would not differ from that required for "iii."

      - - -
      Update
      ( apology/reply to jdb's, immediately below): Yep, agree. To some extent, my comment was OT or redundant, anyway, but someday, some SoPW may find this thread and miss the exception to the general case you asserted.

        True, but i have to stick to 'customers' standards, using arial, helvetica, whatever homemade font etc.
        Second, for html it fixes the problem, but it still is there for pdf-creation. where i use the templating system to create the latex file.
Re: breaking words in a table.
by perl_lover (Chaplain) on Feb 28, 2005 at 09:30 UTC
    You can use nowrap so that email address will not be wrapped.

    <td nowrap>youremailadress@domain.com</td>


    -perl_lover
      Hi perl_lover, i want it to be wrapped.
      TedPride, that't an idea i'll try.
      **update**

      Ok, TedPride, that solved the problem! i now insert the tag on every n'th position of the string. This works perfect. Thanks! (i did not know this html-tag). (at least for the html portion...) Now only for the latex stuff, i really am curious on how to solve this, i'm certin there has to be a way.

        i did not know this html-tag

        It isn't an HTML tag. It is a non-standard extension and won't work in Opera or Safari (or, presumably Konqueror). Gecko (since Mozilla 1.6) and IE do implement it though.