|
|||||||||||||||||||||||||||||||||||||||||||
|
Template::Plugin::String |
|
|
||||
# create String objects via USE directive [% USE String %] [% USE String 'initial text' %] [% USE String text => 'initial text' %] # or from an existing String via new() [% newstring = String.new %] [% newstring = String.new('newstring text') %] [% newstring = String.new( text => 'newstring text' ) %] # or from an existing String via copy() [% newstring = String.copy %] # append text to string [% String.append('text to append') %] # format left, right or center/centre padded [% String.left(20) %] [% String.right(20) %] [% String.center(20) %] # American spelling [% String.centre(20) %] # European spelling # and various other methods... |
|
||||
This module implements a String class for doing stringy things to text in an object-oriented way. You can create a String object via the USE directive, adding any initial text value as an argument or as the named parameter 'text'. [% USE String %] [% USE String 'initial text' %] [% USE String text='initial text' %] The object created will be referenced as 'String' by default, but you can provide a different variable name for the object to be assigned to: [% USE greeting = String 'Hello World' %] Once you've got a String object, you can use it as a prototype to create other String objects with the new() method. [% USE String %] [% greeting = String.new('Hello World') %] The new() method also accepts an initial text string as an argument or the named parameter 'text'. [% greeting = String.new( text => 'Hello World' ) %] You can also call copy() to create a new String as a copy of the original. [% greet2 = greeting.copy %] The String object has a text() method to return the content of the string. [% greeting.text %] However, it is sufficient to simply print the string and let the overloaded stringification operator call the text() method automatically for you. [% greeting %] Thus, you can treat String objects pretty much like any regular piece of text, interpolating it into other strings, for example: [% msg = "It printed '$greeting' and then dumped core\n" %] You also have the benefit of numerous other methods for manipulating the string. [% msg.append("PS Don't eat the yellow snow") %] Note that all methods operate on and mutate the contents of the string itself. If you want to operate on a copy of the string then simply take a copy first: [% msg.copy.append("PS Don't eat the yellow snow") %] These methods return a reference to the String object itself. This allows you to chain multiple methods together. [% msg.copy.append('foo').right(72) %] It also means that in the above examples, the String is returned which causes the text() method to be called, which results in the new value of the string being printed. To suppress printing of the string, you can use the CALL directive. [% foo = String.new('foo') %] [% foo.append('bar') %] # prints "foobar" [% CALL foo.append('bar') %] # nothing |
|
||||
Construction MethodsThe following methods are used to create new String objects.
Inspection MethodsThese methods are used to inspect the string content or other parameters relevant to the string.
Mutation MethodsThese methods modify the internal value of the string. For example: [% USE str=String('foobar') %] [% str.append('.html') %] # str => 'foobar.html' The value of the String 'str' is now 'foobar.html'. If you don't want to modify the string then simply take a copy first. [% str.copy.append('.html') %] These methods all return a reference to the String object itself. This has two important benefits. The first is that when used as above, the String object 'str' returned by the append() method will be stringified with a call to its text() method. This will return the newly modified string content. In other words, a directive like: [% str.append('.html') %] will update the string and also print the new value. If you just want to update the string but not print the new value then use CALL. [% CALL str.append('.html') %] The other benefit of these methods returning a reference to the String is that you can chain as many different method calls together as you like. For example: [% String.append('.html').trim.format(href) %] Here are the methods:
|
|
||||
Andy Wardley <[email protected]> |
|
||||
2.34, distributed as part of the Template Toolkit version 2.14, released on 04 October 2004. |
|
||||
Copyright (C) 1996-2004 Andy Wardley. All Rights Reserved. Copyright (C) 1998-2002 Canon Research Centre Europe Ltd. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. |
|
||||
|
|
|
|
|
|
|
|
|
http://www.template-toolkit.org/ | ||