[Templates] substr vmethod

David Vergin dvergin@igc.org
Wed, 09 Aug 2006 08:22:30 -0700


on 8/9/2006 7:28 AM Jonathan Mangin said the following:
 > I need to display the first 34 chars. of a template
 > variable.

There's probably a less wasteful way (and it isn't a way to grab a 
substring from the *middle* of a string), but for the purpose you 
describe I just do:

    myvar.chunk(34).0

That is, the zero-th chunk of length 34 from the string stored as myvar.

Here's most of the description of chunk from:
http://www.template-toolkit.org/docs/plain/Manual/VMethods.html

------------------------8<-------------------------

chunk(size)

Splits the value into a list of chunks of a certain size.

     [% ccard_no = "1234567824683579";
        ccard_no.chunk(4).join
     %]

Output:

     1234 5678 2468 3579

---------------------------------------------------