In my quest of Refactoring webcode to use templates, I have hit an issue which I can solve in a couple of ways. But...I am trying to keep the logic separate from the display and whichever way I look at, I am struggling to maintain that separation.

On an HTML page I have a block of text pulled from a database. The length of the text is not known until runtime. If the text is too long, it is split into a visible and a hidden section with the display attribute toggled by a bit of Javascript.

<script> function toggleDisplay(el, txt) { if (document.getElementById(txt).style.display == 'none') { document.getElementById(txt).style.display = 'inline'; el.innerHTML = ' [read less...]'; } else { document.getElementById(txt).style.display = 'none'; el.innerHTML = ' [read more...]'; } } </script>
The code that does the shortening and formatting of the text will become a function in a module but it currently is a subroutine in the script that outputs all the HTML.
sub abstract { my ($visible, $hidden); if (length $_[0] < 150) { $visible = $_[0]; } else { my @words = split / +/, $_[0]; my $flag = 0; while (length $visible < 150) { $visible .= ' ' if $flag; $visible .= shift @words; $flag = 1; } $hidden = join ' ', @words; $visible .= qq[ <span id="t2" style="display:none">$hidden</sp +an> <span onClick="toggleDisplay(this, 't2');"> [read more ...]</span +>]; } return $visible; }

If I use the same code with a Template, I am going to be passing partly formatted text to the process method. This does not entirely separate the logic from the display as part of the display is created in the Perl code.

The other way I came up with was to use a callback from the template to the Perl function that splits the text. Again, this seems to tie the logic and display together.

Is there an elegant solution that I am overlooking to what must be quite a common problem?


In reply to Splitting long text for Template by Bod

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.