in reply to Splitting long text for Template

Yes, it's a common problem. The 'elegant' solution would be to have a function in the client code (i.e. Javascript) that does the formatting/truncating once handed the complete string by Perl. Or as you say you could enable PERL directives in your template; or you could write a custom filter in Perl which Template would use in the template. Or you could probably use simple conditional IF blocks in the template. None of these are what you might call elegant, but functionality trumps elegance, and I would personally use the last mentioned approach because I dislike programming in JavaScript so much. In my experience accepting a somewhat fuzzy boundary between C and V works best for me.

Hope this helps!


The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: Splitting long text for Template
by choroba (Cardinal) on Feb 07, 2021 at 23:54 UTC
    > you could probably use simple conditional IF blocks in the template

    I fully agree. Especially if you want to hide the rest of the text for a purpose, e.g. show the full text only to the paying customers. Hiding it via JavaScript would still make them have the full text at their disposal. The distinction of a paying/not paying customer is definitely not part of the View's business.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      > The distinction of a paying/not paying customer is definitely not part of the View's business.

      But that's another use case, which would affect the Model too, cause you'd need a table customer for a paywall scenario.

      The OP said "if the text is too long"

      This can also depend on the output media is it ...

      • a large desktop browser?
      • a mobile?
      • for print?
      • ...

      Clearly presentation!

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

      Especially if you want to hide the rest of the text for a purpose, e.g. show the full text only to the paying customers.

      I hadn't even considered that as a possibility! In this case, it is only to keep the amount of text short enough to get lots of entries on a page. The user can expand any they for which they want further information.

      So, something like [% IF text.length > 150 %] - I can't see how to ensure the text is split on a word boundary with a simple IF condition.