Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-25 20:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found